/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /** * Array2ArrayTree * @package * @author Gaurav Jassal * @copyright <a href="http://www.gauravjassal.com">http://www.gauravjassal.com</a> * @version 1.0 * @access public */ class Array2ArrayTree { public $strKey = ""; /** * Array2ArrayTree::__construct() * * @param $arrData Array * @param $arrKey String * @return */ public function __construct($arrData, $arrKey) { $this->arrOriginal = $arrData; $this->strKey = $arrKey; } /** * Array2ArrayTree::makeTree() * * @return Array */ public function makeTree() { $keyPosition = $this->searchKey($this->arrOriginal[$i][$this->strKey]); if ($keyPosition == -1) { $this->addNode($this->arrOriginal[$i]); } else { $this->appendNode($this->arrOriginal[$i], $keyPosition); } } return $this->arrDummy; } /** * Array2ArrayTree::searchKey() * * @param $strCurrentValue String * @return */ function searchKey($strCurrentValue) { if ($this->arrDummy[$i][0][$this->strKey] == $strCurrentValue) { return $i; } } return - 1; } /** * Array2ArrayTree::addNode() * * @param $arrNode Array * @return */ function addNode($arrNode) { } /** * Array2ArrayTree::appendNode() * * @param $arrNode Array * @param $keyPosition Integer * @return */ function appendNode($arrNode, $keyPosition) { } } ?> Usage <?php require_once("array2arraytree.php"); "product_id" => "007", "product_name" => "Blackberry R-900 Mobile", "product_price" => "£450", "product_status" =>"1", "product_category" =>"Mobile" ), "product_id" => "033", "product_name" => "8 GB Pendrive", "product_price" => "£14.99", "product_status" => "0", "product_category" => "Computers" ), "product_id" => "033", "product_name" => "The White Tiger – Aravind Adiga", "product_price" => "£29.99", "product_status" => "1", "product_category" => "Books" ), "product_id" => "4501", "product_name" => "The Final Reckoning - Sam Bourne", "product_price" => "£19.99", "product_status" => "0", "product_category" => "Books" ), "product_id" => "001", "product_name" => "Wespro Multi-SIM & Touch-screen Mobile", "product_price" => "£150", "product_status" => "1", "product_category" => "Mobile" ), "product_id" => "004", "product_name" => "Sigmatel MP4/MP3 + Camera Mobile", "product_price" => "£150", "product_status" => "1", "product_category" => "Mobile" ), "product_id" => "034", "product_name" => "The Final Reckoning - Sam Bourne", "product_price" => "£15.79", "product_status" => "0", "product_category" => "Books" ), "product_id" => "334", "product_name" => "250 GB Portable Hard Drive", "product_price" => "£79.99", "product_status" => "1", "product_category" => "Computers" ) ); $objTree=new Array2ArrayTree($arrProducts,"product_category"); $arrTree=$objTree->makeTree(); print("<pre>"); print("</pre>"); ?>
URL: http://www.gauravjassal.com/index.php/site/entry/filter_array_into_categorized_format/