设置子代父区域后,对多维数组进行递归排序 [英] Sort multidimensional arrays recursively after setting up child-parent realtions

查看:125
本文介绍了设置子代父区域后,对多维数组进行递归排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据库结构:

I have a database structure like this:

ID   name         sort   parent
1    item1        1      0
2    subitem1     2      1
3    subsubitem1  1      2
4    subitem2     1      1

我将数据库写入数组

array (size=4)
  0 => 
    array (size=4)
      'id' => string '1' (length=1)
      'name' => string 'item1' (length=5)
      'parent' => string '0' (length=1)
      'sort' => string '1' (length=1)
  1 => 
    array (size=4)
      'id' => string '2' (length=1)
      'name' => string 'subitem1' (length=8)
      'parent' => string '1' (length=1)
      'sort' => string '2' (length=1)
  2 => 
    array (size=4)
      'id' => string '3' (length=1)
      'name' => string 'subsubitem1' (length=11)
      'parent' => string '2' (length=1)
      'sort' => string '1' (length=1)
  3 => 
    array (size=4)
      'id' => string '4' (length=1)
      'name' => string 'subitem2' (length=8)
      'parent' => string '1' (length=1)
      'sort' => string '1' (length=1)

并使用以下功能重构该数组以建立子代关系:

and restructure that array to set up child-parent relations with this function:

function generateNavArray($arr, $parent = 0)
{
    $items = Array();
    foreach($arr as $item)
    {
        if($item['parent'] == $parent)
        {
            $item['child'] = isset($item['child']) ? $item['child'] : GenerateNavArray($arr, $item['id']);
            $items[] = $item;
        }
    }
    return $items;
}

生成的数组看起来像这样

and the generated array looks like this

array (size=1)
  0 => 
    array (size=5)
      'id' => string '1' (length=1)
      'name' => string 'item1' (length=5)
      'parent' => string '0' (length=1)
      'sort' => string '1' (length=1)
      'child' => 
        array (size=2)
          0 => 
            array (size=5)
              'id' => string '2' (length=1)
              'name' => string 'subitem' (length=4)
              'parent' => string '1' (length=1)
              'sort' => string '2' (length=1)
              'child' => 
                array (size=1)
                  0 => 
                    array (size=5)
                      'id' => string '3' (length=1)
                      'name' => string 'subsubitem1' (length=11)
                      'parent' => string '2' (length=1)
                      'sort' => string '1' (length=1)
                      'child' => 
                        array (size=0)
                          empty
          1 => 
            array (size=5)
              'id' => string '3' (length=1)
              'name' => string 'subitem2' (length=8)
              'parent' => string '1' (length=1)
              'sort' => string '1' (length=1)
              'child' => 
                array (size=0)
                  empty

现在我需要按排序值对数组的每个维度进行排序,(我的真实数组"具有比此数组更多的子数组). 我玩过multisort,但似乎找不到解决方法 有什么想法吗?

now i need to sort every dimension of the array by the sort value, (my "real array" has more subarrays then this one). i played around with multisort but i can't seem to find the solution any ideas?

推荐答案

在构建多维数组之前,对数组为1维时进行排序.如果您使用查询,甚至更好,请在此处对其进行排序.按父排序,然后排序.当您构建多维数组时,每个子级都将追加到父级.如果它们已经按照正确的顺序排列,则它们将以相同的顺序结束.

Sort the array when it is 1 dimension before you build the multi-dimensional array. Even better if you are using a query, sort it there. Sort by parent then sort. When you build your multidimensional array, each child will be appended to the parent. If they are already in the correct order, they will end up in the same order.

这篇关于设置子代父区域后,对多维数组进行递归排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆