1

I have array with following records

   Array
(
    [0] => Array
        (
            [id] => 76
            [category_name] => Baby & children
            [category_path] => 76
        )

    [1] => Array
        (
            [id] => 77
            [category_name] => Baby bathing & potting
            [category_path] => 76,77
        )

    [2] => Array
        (
            [id] => 78
            [category_name] => Baby baths
            [category_path] => 76,77,78
        )

    [3] => Array
        (
            [id] => 79
            [category_name] => Baby safety
            [category_path] => 76,79
        )

    [4] => Array
        (
            [id] => 80
            [category_name] => Baby video monitors
            [category_path] => 76,79,80
        )

    [5] => Array
        (
            [id] => 81
            [category_name] => Socket covers
            [category_path] => 76,79,81
        )

    [6] => Array
        (
            [id] => 82
            [category_name] => Baby sleeping & bedding
            [category_path] => 76,82
        )

    [7] => Array
        (
            [id] => 83
            [category_name] => Baby sleeping bags
            [category_path] => 76,82,83
        )

    [8] => Array
        (
            [id] => 84
            [category_name] => Feeding &  diapering & nursing
            [category_path] => 76,84
        )

    [9] => Array
        (
            [id] => 85
            [category_name] => Baby scales
            [category_path] => 76,84,85
        )

    [10] => Array
        (
            [id] => 86
            [category_name] => Bottle nipples
            [category_path] => 76,84,86
        )

    [11] => Array
        (
            [id] => 87
            [category_name] => Changing mats
            [category_path] => 76,84,87
        )

    [12] => Array
        (
            [id] => 88
            [category_name] => Feeding bottles
            [category_path] => 76,84,88
        )

    [13] => Array
        (
            [id] => 89
            [category_name] => Toys & accessories
            [category_path] => 76,89
        )

    [14] => Array
        (
            [id] => 90
            [category_name] => Children toy figures
            [category_path] => 76,89,90
        )

)

How can I create a tree from this array.

I want to create a menu with this categories (records).

Baby & children > Baby bathing & potting

Baby & children > Baby bathing & potting > Baby baths

Baby & children > Baby bathing & potting > Baby safety

...

3 Answers 3

1

According to your given array:

$array =  array(
   0 => Array
     (
         'id' => 76,
         'category_name' => "Baby & children",
         'category_path' => "76",
     ),

   1 => Array
     (
         'id' => 77,
         'category_name' => "Baby bathing & potting",
         'category_path' => "76,77",
     ),

   2 => Array
     (
         'id' => 78,
         'category_name' => "Baby baths",
         'category_path' => "76,77,78"
     ),

   3 => Array
     (
         'id' => 79,
         'category_name' => "Baby safety",
         'category_path' => "76,79"
     ),

   4 => Array
     (
         'id' => 80,
         'category_name' => "Baby video monitors",
         'category_path' => "76,79,80"
     ),

   5 => Array
     (
         'id' => 81,
         'category_name' => "Socket covers",
         'category_path' => "76,79,81"
     ),

   6 => Array
     (
         'id' => 82,
         'category_name' => "Baby sleeping & bedding",
         'category_path' => "76,82"
     ),

   7 => Array
     (
         'id' => 83,
         'category_name' => "Baby sleeping bags",
         'category_path' => "76,82,83"
     ),

   8 => Array
     (
         'id' => 84,
         'category_name' => "Feeding &  diapering & nursing",
         'category_path' => "76,84"
     ),

   9 => Array
     (
         'id' => 85,
          'category_name' => "Baby scales",
          'category_path' => "76,84,85"
      ),

   10 => Array
     (
         'id' => 86,
         'category_name' => "Bottle nipples",
         'category_path' => "76,84,86"
     ),

   11 => Array
     (
         'id' => 87,
         'category_name' => "Changing mats",
         'category_path' => "76,84,87"
     ),

   12 => Array
     (
         'id' => 88,
         'category_name' => "Feeding bottles",
         'category_path' => "76,84,88"
     ),

   13 => Array
     (
         'id' => 89,
         'category_name' => "Toys & accessories",
         'category_path' => "76,89"
     ),

   14 => Array
     (
          'id' => 90,
            'category_name' => "Children toy figures",
            'category_path' => "76,89,90"
    )
);

You can use something like this:

createMenu($array, getRootItems($array), $menu);
echo $menu;

/*
    Recursive method to create the menu
*/
function createMenu(&$sourceArray, $menuElements, &$menu, $dept=0)
{
    $dept++;

    $menu .= '<ul>';
    if(count($menuElements) > 0)
    {
        foreach($menuElements as $menuElement)
        {
            $menu .= '<li>'.$menuElement['category_name'];

            $childItems = getChildItems($sourceArray, $menuElement['id'], $dept);
            if(count($childItems) > 0)
            {
                createMenu($sourceArray, $childItems, $menu, $dept);
            }
            $menu .= '</li>';   
        }
    }
    $menu .= '</ul>';
}

/*
    Function to get all root elements for the first level menu items
*/
function getRootItems($menuElements)
{
    $rootItems = array();
    foreach($menuElements as $arrayItem)
    {
        if(str_replace(',','',$arrayItem['category_path']) == $arrayItem['id'])
        {
            $rootItems[$arrayItem['id']] = $arrayItem;
        }
    }

    return $rootItems;
}

/*
    Function get the children items of the a certain id
*/
function getChildItems(&$sourceArray, $parentId, $dept)
{
    $childItems = array();

    foreach($sourceArray as $arrayItem)
    {
        if(!isset($arrayItem['category_array']) || !empty($arrayItem['category_array']))
        {
            $arrayItem['category_array'] = explode(',',$arrayItem['category_path']);
        }

        if(in_array($parentId,$arrayItem['category_array']) && count($arrayItem['category_array']) == $dept+1)
        {   
            $childItems[$arrayItem['id']] = $arrayItem;
        }
    }

    return $childItems;
}

Output is:

Baby & children
    Baby bathing & potting
        Baby baths

    Baby safety
        Baby video monitors
        Socket covers

    Baby sleeping & bedding
        Baby sleeping bags

    Feeding & diapering & nursing
        Baby scales
        Bottle nipples
        Changing mats
        Feeding bottles

    Toys & accessories
        Children toy figures

Note that this is not the same as you said in your question. That's because the structure of your array doesn't match with the output you want. E.g. if you want Baby & children > Baby bathing & potting > Baby safety you need to add id 77 to the category_path of "Baby safety" (like "Baby baths").

Sign up to request clarification or add additional context in comments.

Comments

0

I know it's a bit dirty but it works.

$arrayMenu = array(
    array('id' => 76, 'category_name' => 'Baby & children', 'category_path' => '76' ),
    array('id' => 77, 'category_name' => 'Baby bathing & potting', 'category_path' => '76,77' ),
    array('id' => 78, 'category_name' => 'Baby baths', 'category_path' => '76,77,78' )
);

$tmp = array();

foreach($arrayMenu as $item)
{
    $tmp[$item['id']] = $item['category_name'];
}

$keys = array_keys($tmp);
$values = array_values($tmp);

foreach($arrayMenu as $item)
{
    echo str_replace($keys, $values, str_replace(',',' > ', $item['category_path'])).'<br>';
}

Comments

0

Reformat the array when you create it. Create multidimensional array(array inside array) to nest the menu groups under their primary groups.

array
(
 'Baby & children' => array
     (
        'Baby bathing & potting' => array (
               'Baby baths' = 1,
               'Baby safety' = 2 
        )
     )
)

a basic mock up array. Then you can use:

foreach (array as $key => $value) {
  echo $key;
  foreach ($value as $subkey => $subvalue) {
    echo $subkey;
    foreach ($subvalueas $index => $data) {
      echo $index;
    }
  }
}

To access each key and its values. A basic example that doesnt provide any keys to "remember" last selected on page change etc.

Organizing the array to a logic tree before using it saves time and makes it easier to get them in correct order.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.