【问题标题】:Format a nested set model result into PHP array将嵌套集模型结果格式化为 PHP 数组
【发布时间】:2015-04-20 16:55:35
【问题描述】:

希望创建一个函数,根据级别(类别 [0]、子类别 [1]、类型 [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
        )

)

有什么想法吗?

【问题讨论】:

    标签: php arrays multidimensional-array nested


    【解决方案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>";
    }
    ?>
    

    【讨论】:

      【解决方案2】:

      如果我理解正确,请尝试以下解决方案:

       <?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);
      ?>
      

      【讨论】:

        猜你喜欢
        • 2013-06-04
        • 1970-01-01
        • 1970-01-01
        • 2019-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-28
        相关资源
        最近更新 更多