Friday 26 April 2013

Function for create parent child Tree FROM Array

<?php
/* Function for create parent child Tree*/
function buildTree(array $elements, $parentId = '')
{
        $branch = array();
        foreach ($elements as $element)
    {
            if ($element['parent'] == $parentId)
        {
            $children = buildTree($elements, $element['id']);
            if ($children) {
            $element['children'] = $children;
            }
            $branch[] = $element;
        }
    }
    return $branch;
}   

$mainarray = array(
"0"=>array("id"=>1,"orderby"=>"10","title"=>"Home","link"=>"home.php","active"=>"Y","parent"=>""),
"1"=>array("id"=>2,"orderby"=>"20","title"=>"Item1","link"=>"item.php","active"=>"Y","parent"=>""),
"2"=>array("id"=>3,"orderby"=>"30","title"=>"Item2","link"=>"item2.php","active"=>"Y","parent"=>""),
"3"=>array("id"=>14,"orderby"=>"80","title"=>"Item3","link"=>"item3.php","active"=>"Y","parent"=>""),
"4"=>array("id"=>4,"orderby"=>"40","title"=>"Sub1Item1","link"=>"sub1item.php","active"=>"Y","parent"=>"2"),
"5"=>array("id"=>5,"orderby"=>"50","title"=>"Sub2Item1","link"=>"sub2item.php","active"=>"Y","parent"=>"2"),
"6"=>array("id"=>6,"orderby"=>"60","title"=>"Sub3Item1","link"=>"sub3item.php","active"=>"Y","parent"=>"2"),
"7"=>array("id"=>10,"orderby"=>"70","title"=>"Sub2Sub1Item1","link"=>"sub1item1.php","active"=>"Y","parent"=>"4"),
);
$mainarray = buildTree($mainarray);
echo "<pre>";print_r($mainarray); exit;
?>

No comments:

Post a Comment

List OF BANK PAN Numbers

List OF BANK PAN Numbers Bank/Home Loan Providers PAN Number Allahabad Bank AACCA8464F Andhra Bank AABCA7375C Axis Bank...