Gaurav Jassal
<?php Ideas are my bread n butter. I mold and shape them into living breathing interactive experiences and captivate. I believe that great interactive ideas drive on the tension between design and code. ?>
Info
(contact)

+44.7518.599.587
hello[at]gauravjassal[dot]com

Filter Array into Categorized format

This simple yet powerful class to filters an array and breaks in to a categorized array format.You can use this class if you are dealing with large and complex array and want to convert it into and simple array that is easy to understand and use. Recently i required a function that can categorize an array and break it into categorize format.I couldn't find any core function to do that so i decided to create my own class that can do this job for me. I have also added an example to demonstrate the usage to its benefit. Make sure you comment it if you find it useful.

Original Array

$arrProducts=array(
        array(
        "product_id" 			=> "007",
        "product_name"      	=> "Blackberry R-900 Mobile",
        "product_price" 		=> "£450",
        "product_status"		=>"1",
        "product_category"		=>"Mobile"
        ),
		array(
        "product_id" 			=> "033",
        "product_name"      	=> "8 GB Pendrive",
        "product_price" 		=> "£14.99",
        "product_status"		=> "0",
        "product_category"		=> "Computers"
        )
);

Array2ArrayTree Class

Here is the class that converts the array to and organize format. The constructor take two arguments first the Array you want to format and the second category key you want to filter by.Then you have to call makeTree() function to format the array. The makeTree() function will return you the organized array.

<?php
/**
 * Array2ArrayTree
 * @package   
 * @author Gaurav Jassal
 * @copyright http://www.gauravjassal.com
 * @version 1.0
 * @access public
 */
class Array2ArrayTree
{
    public $arrOriginal = array();
    public $arrDummy = array();
    public $strKey = "";
    /**
     * Array2ArrayTree::__construct()
     * 
     * @param $arrData Array
     * @param $arrKey String 
     * @return
     */
    public function __construct($arrData, $arrKey)
    {
        $this->arrOriginal = $arrData;
        $this->strKey = $arrKey;
        $this->arrDummy = array();
    }

    /**
     * Array2ArrayTree::makeTree()
     * 
     * @return Array
     */
    public function makeTree()
    {
        for ($i = 0; $i <= sizeof($this->arrOriginal) - 1; $i++) {
            $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)
    {
        for ($i = 0; $i <= sizeof($this->arrDummy) - 1; $i++) {
            if ($this->arrDummy[$i][0][$this->strKey] == $strCurrentValue) {
                return $i;
            }
        }
        return - 1;
    }
    /**
     * Array2ArrayTree::addNode()
     * 
     * @param $arrNode Array
     * @return
     */
    function addNode($arrNode)
    {
        $this->arrDummy[sizeof($this->arrDummy)][0] = $arrNode;
    }
    /**
     * Array2ArrayTree::appendNode()
     * 
     * @param $arrNode Array
     * @param $keyPosition Integer
     * @return
     */
    function appendNode($arrNode, $keyPosition)
    {
        array_push($this->arrDummy[$keyPosition], $arrNode);
    }
}
?>
Usage
<?php
require_once("array2arraytree.php");
$arrProducts=array(
                array(
                "product_id" 			=> "007",
                "product_name"      	=> "Blackberry R-900 Mobile",
                "product_price" 		=> "£450",
                "product_status"		=>"1",
                "product_category"		=>"Mobile"
                ),
				array(
                "product_id" 			=> "033",
                "product_name"      	=> "8 GB Pendrive",
                "product_price" 		=> "£14.99",
                "product_status"		=> "0",
                "product_category"		=> "Computers"
                ),
				array(
                "product_id" 			=> "033",
                "product_name"      	=> "The White Tiger – Aravind Adiga",
                "product_price" 		=> "£29.99",
                "product_status"		=> "1",
                "product_category"		=> "Books"
                ),
				array(
                "product_id" 			=> "4501",
                "product_name"      	=> "The Final Reckoning - Sam Bourne",
                "product_price" 		=> "£19.99",
                "product_status"		=> "0",
                "product_category"		=> "Books"
                ),
				array(
                "product_id" 			=> "001",
                "product_name"      	=> "Wespro Multi-SIM & Touch-screen Mobile",
                "product_price" 		=> "£150",
                "product_status"		=> "1",
                "product_category"		=> "Mobile"
                ),
				array(
                "product_id" 			=> "004",
                "product_name"      	=> "Sigmatel MP4/MP3 + Camera Mobile",
                "product_price" 		=> "£150",
                "product_status"		=> "1",
                "product_category"		=> "Mobile"
                ),
				array(
                "product_id" 			=> "034",
                "product_name"      	=> "The Final Reckoning - Sam Bourne",
                "product_price" 		=> "£15.79",
                "product_status"		=> "0",
                "product_category"		=> "Books"
                ),
				array(
                "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("
");
print_r($arrTree);
print("
"); ?>

Result Array

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [product_id] => 007
                    [product_name] => Blackberry R-900 Mobile
                    [product_price] => £450
                    [product_status] => 1
                    [product_category] => Mobile
                )

            [1] => Array
                (
                    [product_id] => 001
                    [product_name] => Wespro Multi-SIM & Touch-screen Mobile
                    [product_price] => £150
                    [product_status] => 1
                    [product_category] => Mobile
                )

            [2] => Array
                (
                    [product_id] => 004
                    [product_name] => Sigmatel MP4/MP3 + Camera Mobile
                    [product_price] => £150
                    [product_status] => 1
                    [product_category] => Mobile
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [product_id] => 033
                    [product_name] => 8 GB Pendrive
                    [product_price] => £14.99
                    [product_status] => 0
                    [product_category] => Computers
                )

            [1] => Array
                (
                    [product_id] => 334
                    [product_name] => 250 GB Portable Hard Drive
                    [product_price] => £79.99
                    [product_status] => 1
                    [product_category] => Computers
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [product_id] => 033
                    [product_name] => The White Tiger – Aravind Adiga
                    [product_price] => £29.99
                    [product_status] => 1
                    [product_category] => Books
                )

            [1] => Array
                (
                    [product_id] => 4501
                    [product_name] => The Final Reckoning - Sam Bourne
                    [product_price] => £19.99
                    [product_status] => 0
                    [product_category] => Books
                )

            [2] => Array
                (
                    [product_id] => 034
                    [product_name] => The Final Reckoning - Sam Bourne
                    [product_price] => £15.79
                    [product_status] => 0
                    [product_category] => Books
                )

        )

)

You can download the files here

Enjoy this post? Share it with others.

Name:

Email:

Smileys

Remember my personal information

Submit the word you see below:


Previous entry: Why use Constraints in SQL ?

Wise Words

"If we know who we are, we will not try to become someone else in order to have value and meaning in our lives. If we don't know who we are, we will try to become someone who someone else wants us to be."

- Robert McGee

Social Things

Where you can fine me !!

View Gaurav Jassal's profile on LinkedIn

View Gaurav Jassal's profile on LinkedIn View Gaurav Jassal's profile on Delicious View Gaurav Jassal's profile on Digg View Gaurav Jassal's profile on Facebook View Gaurav Jassal's profile on feedburner View Gaurav Jassal's profile on flickr View Gaurav Jassal's profile on lastfm View Gaurav Jassal's profile on netvibes View Gaurav Jassal's profile on twitter RSS

Search Box


Advanced Search