Filter Array into Categorized format


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * Array2ArrayTree
  4.  * @package
  5.  * @author Gaurav Jassal
  6.  * @copyright <a href="http://www.gauravjassal.com">http://www.gauravjassal.com</a>
  7.  * @version 1.0
  8.  * @access public
  9.  */
  10. class Array2ArrayTree
  11. {
  12. public $arrOriginal = array();
  13. public $arrDummy = array();
  14. public $strKey = "";
  15. /**
  16.   * Array2ArrayTree::__construct()
  17.   *
  18.   * @param $arrData Array
  19.   * @param $arrKey String
  20.   * @return
  21.   */
  22. public function __construct($arrData, $arrKey)
  23. {
  24. $this->arrOriginal = $arrData;
  25. $this->strKey = $arrKey;
  26. $this->arrDummy = array();
  27. }
  28.  
  29. /**
  30.   * Array2ArrayTree::makeTree()
  31.   *
  32.   * @return Array
  33.   */
  34. public function makeTree()
  35. {
  36. for ($i = 0; $i <= sizeof($this->arrOriginal) - 1; $i++) {
  37. $keyPosition = $this->searchKey($this->arrOriginal[$i][$this->strKey]);
  38. if ($keyPosition == -1) {
  39. $this->addNode($this->arrOriginal[$i]);
  40. } else {
  41. $this->appendNode($this->arrOriginal[$i], $keyPosition);
  42. }
  43. }
  44. return $this->arrDummy;
  45. }
  46. /**
  47.   * Array2ArrayTree::searchKey()
  48.   *
  49.   * @param $strCurrentValue String
  50.   * @return
  51.   */
  52. function searchKey($strCurrentValue)
  53. {
  54. for ($i = 0; $i <= sizeof($this->arrDummy) - 1; $i++) {
  55. if ($this->arrDummy[$i][0][$this->strKey] == $strCurrentValue) {
  56. return $i;
  57. }
  58. }
  59. return - 1;
  60. }
  61. /**
  62.   * Array2ArrayTree::addNode()
  63.   *
  64.   * @param $arrNode Array
  65.   * @return
  66.   */
  67. function addNode($arrNode)
  68. {
  69. $this->arrDummy[sizeof($this->arrDummy)][0] = $arrNode;
  70. }
  71. /**
  72.   * Array2ArrayTree::appendNode()
  73.   *
  74.   * @param $arrNode Array
  75.   * @param $keyPosition Integer
  76.   * @return
  77.   */
  78. function appendNode($arrNode, $keyPosition)
  79. {
  80. array_push($this->arrDummy[$keyPosition], $arrNode);
  81. }
  82. }
  83. ?>
  84. Usage
  85.  
  86. <?php
  87. require_once("array2arraytree.php");
  88. $arrProducts=array(
  89. "product_id" => "007",
  90. "product_name" => "Blackberry R-900 Mobile",
  91. "product_price" => "£450",
  92. "product_status" =>"1",
  93. "product_category" =>"Mobile"
  94. ),
  95. "product_id" => "033",
  96. "product_name" => "8 GB Pendrive",
  97. "product_price" => "£14.99",
  98. "product_status" => "0",
  99. "product_category" => "Computers"
  100. ),
  101. "product_id" => "033",
  102. "product_name" => "The White Tiger – Aravind Adiga",
  103. "product_price" => "£29.99",
  104. "product_status" => "1",
  105. "product_category" => "Books"
  106. ),
  107. "product_id" => "4501",
  108. "product_name" => "The Final Reckoning - Sam Bourne",
  109. "product_price" => "£19.99",
  110. "product_status" => "0",
  111. "product_category" => "Books"
  112. ),
  113. "product_id" => "001",
  114. "product_name" => "Wespro Multi-SIM & Touch-screen Mobile",
  115. "product_price" => "£150",
  116. "product_status" => "1",
  117. "product_category" => "Mobile"
  118. ),
  119. "product_id" => "004",
  120. "product_name" => "Sigmatel MP4/MP3 + Camera Mobile",
  121. "product_price" => "£150",
  122. "product_status" => "1",
  123. "product_category" => "Mobile"
  124. ),
  125. "product_id" => "034",
  126. "product_name" => "The Final Reckoning - Sam Bourne",
  127. "product_price" => "£15.79",
  128. "product_status" => "0",
  129. "product_category" => "Books"
  130. ),
  131. "product_id" => "334",
  132. "product_name" => "250 GB Portable Hard Drive",
  133. "product_price" => "£79.99",
  134. "product_status" => "1",
  135. "product_category" => "Computers"
  136. )
  137. );
  138.  
  139. $objTree=new Array2ArrayTree($arrProducts,"product_category");
  140. $arrTree=$objTree->makeTree();
  141. print("<pre>");
  142. print_r($arrTree);
  143. print("</pre>");
  144. ?>

URL: http://www.gauravjassal.com/index.php/site/entry/filter_array_into_categorized_format/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.