将嵌套集模型结果格式化为PHP数组

时间:2015-04-15 06:25:56

标签: php arrays multidimensional-array nested

希望创建一个函数,根据级别(categories [0],sub_categories [1],types [2])将嵌套集模型排列到数组中。

categories[0]
Alcohol |
        |     sub_categories[1]
        |—————Beer
        |
        |—————Spirts
        |
        |—————Cider
        |
        |—————Wine
        |     |
        |     |     types[2]
        |     |—————White wine
        |     |
        |     |—————Red wine
        |
Bvrages |
        |      sub_categories[1]
        |————— Soft Drinks

我正在看一个较旧的帖子,它让我接近但只转移了标题而不是整个数组How do I format Nested Set Model data into an array?

我想要实现的是类似于上面的结构。请记住,我想要移动所有数据而不仅仅是标题。

我查询的当前输出如下:

    Array
(
    [0] => Array
        (
            [id] => 1
            [title] => alcohol
            [level] => 0
            [uri] => alcohol
            [count] => 100
        )

    [1] => Array
        (
            [id] => 2
            [title] => beer
            [level] => 1
            [uri] => beer
            [count] => 50
        )

    [2] => Array
        (
            [id] => 3
            [title] => cider
            [level] => 1
            [uri] => cider
            [count] => 20
        )

    [3] => Array
        (
            [id] => 4
            [title] => wine
            [level] => 1
            [uri] => wine
            [count] => 20
        )

    [4] => Array
        (
            [id] => 6
            [title] => white wine
            [level] => 2
            [uri] => white-wine
            [count] => 5
        )

    [5] => Array
        (
            [id] => 7
            [title] => red wine
            [level] => 2
            [uri] => red-wine
            [count] => 15
        )

    [6] => Array
        (
            [id] => 8
            [title] => spirits
            [level] => 1
            [uri] => spirits
            [count] => 5
        )

    [7] => Array
        (
            [id] => 9
            [title] => Beverages
            [level] => 0
            [uri] => beverages
            [count] => 50
        )

    [8] => Array
        (
            [id] => 10
            [title] => soft drinks
            [level] => 1
            [uri] => soft-drink
            [count] => 10
        )

)

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

如果我理解你的话,请尝试这个解决方案:

 <?php
 $data=Array(
    0 => Array(
            'id' => 1,
            'title' => 'alcohol',
            'level' => 0,
            'uri' => 'alcohol',
            'count' => 100,
        ),
    1 => Array(
            'id' => 2,
            'title' => 'beer',
            'level' => 1,
            'uri' => 'beer',
            'count' => 50,
        ),
    2 => Array(
            'id' => 3,
            'title' => 'cider',
            'level' => 1,
            'uri' => 'cider',
            'count' => 20,
        ),
    3 => Array(
            'id' => 4,
            'title' => 'wine,',
            'level' => 1,
            'uri' => 'wine',
            'count' => 20,
        ),
    4 => Array(
            'id' => 6,
            'title' => 'white wine',
            'level' => 2,
            'uri' => 'white-wine',
            'count' => 5,
        ),
    5 => Array(
            'id' => 7,
            'title' => 'red wine',
            'level' => 2,
            'uri' => 'red-wine',
            'count' => 15,
        ),
    6 => Array(
            'id' => 8,
            'title' => 'spirits',
            'level' => 1,
            'uri' => 'spirits',
            'count' => 5,
        ),
    7 => Array(
            'id' => 9,
            'title' => 'Beverages',
            'level' => 0,
            'uri' => 'beverages',
            'count' => 50,
        ),

    8 => Array(
            'id' => 10,
            'title' => 'soft drinks',
            'level' => 1,
            'uri' => 'soft-drink',
            'count' => 10,
        )
);

$cats=array();
$prev_level=1;
$index0='';
$index1=0;
foreach($data as $dat){
    switch($dat['level']){
        case 0:
            $cats[$dat['uri']]=array(
                'name'=>$dat['title'],
                'sub_categories'=>array()
            );
            $index0=$dat['uri'];
            break;
        case 1:
            if($prev_level<1)
                $index1=0;
            else
                $index1++;
            $cats[$index0]['sub_categories'][$index1]=array(
                'name'=>$dat['title']
            );
            break;
        case 2:
            if($prev_level<2){
                $cats[$index0]['sub_categories'][$index1]['types']=array();
            }
            $cats[$index0]['sub_categories'][$index1]['types'][]=$dat['title'];
            break;
    }
    $prev_level=$dat['level'];
}

print_r($cats);
?>

答案 1 :(得分:1)

尝试使用此代码:

<?php
//Represent your array like this (you may include other data to the each item)
//array must be ordered by 'left' field:

$data = array(
 array("left" => 1, "right" => 10, "name" => "P0"),
 array("left" => 2, "right" => 7, "name" => "P1"),
 array("left" => 3, "right" => 4, "name" => "P11"),
 array("left" => 5, "right" => 6, "name" => "P12"),
 array("left" => 8, "right" => 9, "name" => "P2")
);

//Converter function gets nested sets array and returns nested php array
function nest($arrData){
 $stack = array();
 $arraySet = array();
 foreach( $arrData as $intKey=>$arrValues) {
  $stackSize = count($stack);
  while($stackSize > 0 && $stack[$stackSize-1]['right'] < $arrValues['left']) {
   array_pop($stack);
   $stackSize--;
  }

  $link =& $arraySet;
  for($i=0;$i<$stackSize;$i++) {
   $link =& $link[$stack[$i]['id']]["children"]; //navigate to the proper children array
  }

  $tmp = array_push($link,  array ('item'=>$arrValues,'children'=>array()));
  array_push($stack, array('id' => $tmp-1, 'right' => $arrValues['right']));
 }

 return $arraySet;
}


//Print result
printArray(nest($data));

function printArray($array){
 echo "<ul>";
 foreach ($array as $row){
  $children = $row['children'];
  echo "<li>";
  echo $row['item']['name'];
  if (!empty($children)) printArray($children);
  echo "</li>";
 }
 echo "</ul>";
}
?>