如何从字符串中创建数组树?

时间:2014-11-18 09:26:47

标签: php arrays multidimensional-array menu

我有以下记录的数组

   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
        )

)

如何从此数组创建树。

我想创建一个包含此类别(记录)的菜单。

Baby&儿童>婴儿沐浴&灌封

Baby&儿童>婴儿沐浴&灌封>婴儿浴缸

Baby&儿童>婴儿沐浴&灌封>婴儿安全

...

3 个答案:

答案 0 :(得分:1)

根据你给出的数组:

$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"
    )
);

您可以使用以下内容:

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;
}

输出是:

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

请注意,这与您在问题中所说的不一样。那是因为你的数组结构与你想要的输出不匹配。例如。如果您需要Baby & children > Baby bathing & potting > Baby safety,则需要将77添加到“婴儿安全”的category_path(例如“婴儿浴室”)。

答案 1 :(得分:0)

我知道它有点脏,但它有效。

$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>';
}

答案 2 :(得分:0)

创建阵列时重新格式化阵列。创建多维数组(数组内部的数组)以将菜单组嵌套在其主要组下。

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

一个基本的模拟数组。 然后你可以使用:

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

访问每个密钥及其值。一个基本的例子,没有提供任何键,记住&#34;最后一次选择页面更改等。

在使用之前将数组组织到逻辑树可以节省时间并使其更容易按正确顺序排列。