【问题标题】:Sorting data into mulit-dimensional, hierarchical array with PHP使用 PHP 将数据排序为多维分层数组
【发布时间】:2015-04-02 14:39:14
【问题描述】:

我需要对对象数组进行分层排序。数据如下所示:

array (size=54)
  0 => 
    object(stdClass)[786]
      public 'term_id' => string '1' (length=3)
      public 'name' => string 'Boots' (length=25)
      public 'parent' => string '0' (length=3)
  1 => 
    object(stdClass)[785]
      public 'term_id' => string '2' (length=3)
      public 'name' => string 'Dresses' (length=25)
      public 'parent' => string '1' (length=3)
  2 => 
    object(stdClass)[786]
      public 'term_id' => string '3' (length=3)
      public 'name' => string 'Scarves' (length=25)
      public 'parent' => string '2' (length=3)
  3 => 
    object(stdClass)[785]
      public 'term_id' => string '4' (length=3)
      public 'name' => string 'Gloves' (length=25)
      public 'parent' => string '1' (length=3)

我想创建一个多维数组来显示这种“父子”层次结构。每个对象的parent 属性引用另一个对象的term_id

结果看起来像这样:

array (size=54)
  0 => 
    object(stdClass)[786]
      public 'term_id' => string '1' (length=3)
      public 'name' => string 'Boots' (length=25)
      public 'parent' => string '0' (length=3)
      public 'children' => array (size=2)
            0 => 
                object(stdClass)[785]
                  public 'term_id' => string '2' (length=3)
                  public 'name' => string 'Dresses' (length=25)
                  public 'parent' => string '1' (length=3)
                  public 'children' => (size=1)
                      0 => 
                        object(stdClass)[786]
                          public 'term_id' => string '3' (length=3)
                          public 'name' => string 'Scarves' (length=25)
                          public 'parent' => string '2' (length=3)
            1 =>
                object(stdClass)[785]
                  public 'term_id' => string '4' (length=3)
                  public 'name' => string 'Gloves' (length=25)
                  public 'parent' => string '1' (length=3)      

到目前为止,我已经想出了这个代码:

$sortedCategories = array();
foreach($shopCategories as $shopCategory) {       
    $tmp = $shopCategory;
    foreach($shopCategories as $category) {
        if ($tmp->term_id == $category->parent) {
            $tmp->children[] = $category;
            $sortedCategories[] = $tmp;
        } 
    }
}

,但我无法让它与多级层次结构一起使用。

如何对数据进行排序以达到预期的结果?

【问题讨论】:

    标签: php arrays sorting


    【解决方案1】:

    我会使用递归函数。你正在做的并不是真正的排序。您正在构建一个树形结构。假设您的原始对象位于名为$a 的数组中,并且您希望将新树称为$b。此功能的作用是添加您正在处理的当前父母的孩子的每个人。每次它添加一个孩子时,它也会调用自己来添加该对象的孩子。因此,递归。您从 0 的父级开始,我认为这意味着“没有父级”。

    $b = build_tree($a);
    
    function build_tree(&$a, $parent=0)
    {
        $tmp_array = array();
        foreach($a as $obj)
        {
            if($obj->parent == $parent)
            {
                // The next line adds all children to this object
                $obj->children = build_tree($a, $obj->term_id);
                $tmp_array[] = $obj
            }
        }
        // You *could* sort the temp array here if you wanted.
        return $tmp_array;
    }
    

    【讨论】:

    • 完美运行。我只有一个问题。为什么要通过引用build_tree 函数来传递$a?这是必要的吗?非常感谢!
    • 我不会以任何方式更改 $a,因此没有理由复制整个数组。那只会浪费时间和记忆。
    • 这是一个非常合理的观点。我必须说你在回复中提出的剧本看起来非常简洁明了,但我发现很难在我的脑海中重新走一遍它的步骤。我绝对需要练习..
    • 遍历相当简单:从根(空)节点开始。将所有孩子添加到根。添加子节点时,在添加它之前,将所有子节点添加到该节点。所以,当你添加这些孩子时,在你添加之前,添加他们所有的孩子。另一个注意事项:我在这里使用递归,因为递归发生在函数的中间。如果你曾经写过一个递归函数并且递归在最后,那你就做错了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    相关资源
    最近更新 更多