user3665396
user3665396

Reputation: 191

Sort multidimensional arrays recursively after setting up child-parent realtions

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

I write the database into an array

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

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?

Upvotes: 0

Views: 140

Answers (1)

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15301

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.

Upvotes: 1

Related Questions