【问题标题】:php array hierarchicalphp数组分层
【发布时间】:2011-06-18 19:25:30
【问题描述】:

我有一张这样的桌子:

+----+--------+-------+
| id | parent | title |
+----+--------+-------+
|  1 |   NULL | yek   |
|  2 |   NULL | do    |
|  3 |      1 | se    |
|  4 |      3 | char  |
+----+--------+-------+

我需要像这样获取分层数据数组。最好的方法是什么?

Array
(
    [1] => Array
        (
            [3] => Array
                (
                    [4] => 
                )

        )

    [2] => 
)

请帮帮我。

【问题讨论】:

    标签: php sql database


    【解决方案1】:
    // dummy data $recordset should be retrieved from db
    $recordset = array(array('id'=>1, 'parent'=>NULL, 'title'=>'yek'),
                       array('id'=>2, 'parent'=>NULL, 'title'=>'do'),
                       array('id'=>3, 'parent'=>1, 'title'=>'se'),
                       array('id'=>4, 'parent'=>3, 'title'=>'char'),
                      );
    
    function make_tree($recordset)
    {
        $tree = array();
        foreach($recordset as $record) {
            if ($record['parent'] !== NULL) {
                if (!array_key_exists($record['parent'], $tree) $tree[$record['parent']] = array('record'=>array(), 'children'=>array());
                $tree[$record['parent']]['children'][$record['id']] = $record;
            } else {
                if (!array_key_exists($record['id'], $tree) $tree[$record['id']] = array('record'=>array(), 'children'=>array());
                $tree[$record['id']] = $record;
            }
        }
    
        return $tree;
    }
    

    【讨论】:

    • 虽然解决方案是正确的,但如果层次结构越来越深,它就会失败。那么就需要一个递归函数。如果我错了,请纠正我
    【解决方案2】:

    我会使用ORM 之类的Propel,它内置了对您要求的功能的支持。

    【讨论】:

      【解决方案3】:

      首先,一个新数组,其中的键显示为 id。然后,这个数组构建了图。它以递归方式发生在图上。 (对不起我的英语)

      <?php
      
      function change_index_to_id($array) {
          $result = array();
      
          foreach ($array as $value) {
              $result[$value['id']] = $value;
          }
      
          return $result;
      }
      
      function make_graph($data) {
          $graph = array();
      
          foreach ($data as $id => $value) {
              if (!is_null($value['parent'])) {
                  $graph[$value['parent']][$id] = true;
              } else {
                  $graph[$id] = array();
              }
          }
      
          return $graph;
      }
      
      function make_hierarchical_array($item_id, $graph, $data, $marked_items) {  
          $result = $data[$item_id];
          $marked_items[$item_id] = true;
      
          foreach ($graph[$item_id] as $id => $v) {
              if (isset($graph[$id]) && ! $marked_items[$id]) {
                  $result['childrens'][$id] = make_hierarchical_array($id, $graph, $data, &$marked_items);
              } else {
                  $result['childrens'][$id] = $data[$id];
              }
          }
      
          return $result;
      }
      
      // load data from database or other
      $data = array(
          array(
              'id' => 1,
              'parent' => null,
              'title' => 'yek'
          ),
          array(
              'id' => 2,
              'parent' => null,
              'title' => 'do'
          ),
          array(
              'id' => 3,
              'parent' => 1,
              'title' => 'se'
          ),
          array(
              'id' => 4,
              'parent' => 3,
              'title' => 'char'
          ),
      );
      
      
      $data = change_index_to_id($data);
      $graph = make_graph($data);
      
      $result = array();
      $marked_items = array();
      foreach ($graph as $id => $childs) {
          if ($marked_items[$id] == false) {
              $result[$id] = make_hierarchical_array($id, $graph, $data, &$marked_items);
          }
      }
      print_r($result);
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-16
        • 2012-10-20
        • 2012-08-12
        相关资源
        最近更新 更多