-1

I created an array based on a mysql table

Array ( 
[0] => Array ( [id] => 1 [parent_ID] => 0  ) 
[1] => Array ( [id] => 2 [parent_ID] => 0  ) 
[2] => Array ( [id] => 3 [parent_ID] => 2  ) 
[3] => Array ( [id] => 4 [parent_ID] => 2  ) 
[4] => Array ( [id] => 5 [parent_ID] => 2  ) 
[5] => Array ( [id] => 6 [parent_ID] => 1  ) 
[6] => Array ( [id] => 7 [parent_ID] => 1  ) 
[7] => Array ( [id] => 8 [parent_ID] => 1  ) 
[8] => Array ( [id] => 9 [parent_ID] => 1  ) 

I want to create a new array, where the order of the parent_ID is based on the ID. If the parent_ID from an array is “1”, than it needs to be placed directly after the array that has ID “1”. The output of the new array needs to be like this:

Array ( 
[0] => Array ( [id] => 1 [parent_ID] => 0  ) 
[1] => Array ( [id] => 6 [parent_ID] => 1  ) 
[2] => Array ( [id] => 7 [parent_ID] => 1  ) 
[3] => Array ( [id] => 8 [parent_ID] => 1  ) 
[4] => Array ( [id] => 9 [parent_ID] => 1  ) 
[5] => Array ( [id] => 2 [parent_ID] => 0  ) 
[6] => Array ( [id] => 3 [parent_ID] => 2  ) 
[7] => Array ( [id] => 4 [parent_ID] => 2  ) 
[8] => Array ( [id] => 5 [parent_ID] => 2  ) 

I tried to order my array by using the usort function, but that will only order the parent_ID or the ID column. Is it possible with PHP to sort an array like the example?

Nibboro
  • 39
  • 3
  • 2
    Possible duplicate of [How can I sort arrays and data in PHP?](https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php) – CBroe Mar 27 '18 at 08:37
  • _“I tried to order my array by using the usort function, but that will only order the parent_ID or the ID column.”_ - no, it will sort by whatever criteria _you_ decide to base the decision “is element A lesser, equal or greater than element B” on in your callback function. – CBroe Mar 27 '18 at 08:38
  • Why not just sort the data in the original query? – iainn Mar 27 '18 at 08:41
  • That target sorting order you imply with the proposed result is not exactly easy to get, it appears a bit "unlogical" which means it is hard to implement an algorithm that results in such an order. Why is entry with `id` 2 below all those with `parent_ID` 1? – arkascha Mar 27 '18 at 08:49
  • The parent_ID points to the ID in the same SQL table, the exact order of the ID doesnt matter. – Nibboro Mar 27 '18 at 08:57
  • Sure, but that is not an answer to my question, is it? – arkascha Mar 27 '18 at 09:00
  • The output looks pretty logical to me, but again, [why not just do this in the query](http://sqlfiddle.com/#!9/1052b4/2) – iainn Mar 27 '18 at 09:01

1 Answers1

0

As said in my comment to the question your proposed result is bogus...

But here is some simple algorithm constructing such output:

<?php
$input = [
    ['id' => 1, 'parent_ID' => 0],
    ['id' => 2, 'parent_ID' => 0],
    ['id' => 3, 'parent_ID' => 2],
    ['id' => 4, 'parent_ID' => 2],
    ['id' => 5, 'parent_ID' => 2],
    ['id' => 6, 'parent_ID' => 1],
    ['id' => 7, 'parent_ID' => 1],
    ['id' => 8, 'parent_ID' => 1],
    ['id' => 9, 'parent_ID' => 1]
];
$data = [];
$output = [];

array_walk ($input, function($entry) use (&$data) {
    $data[$entry['parent_ID']][] = $entry;
});
array_walk ($data[0], function($entry) use ($data, &$output) {
    $output[] = $entry;
    foreach ($data[$entry['id']] as $child) {
        $output[] = $child;
    }
});
print_r($output);

The output of executing that obviously is:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent_ID] => 0
        )

    [1] => Array
        (
            [id] => 6
            [parent_ID] => 1
        )

    [2] => Array
        (
            [id] => 7
            [parent_ID] => 1
        )

    [3] => Array
        (
            [id] => 8
            [parent_ID] => 1
        )

    [4] => Array
        (
            [id] => 9
            [parent_ID] => 1
        )

    [5] => Array
        (
            [id] => 2
            [parent_ID] => 0
        )

    [6] => Array
        (
            [id] => 3
            [parent_ID] => 2
        )

    [7] => Array
        (
            [id] => 4
            [parent_ID] => 2
        )

    [8] => Array
        (
            [id] => 5
            [parent_ID] => 2
        )

)

However I would like to make some comments to your situation:

  • you really should not store a value of 0 for the first elements. They do not have a parent, if I get your situation right, so that value should actually be null, not 0.
  • you should try to order your data right in the SQL query
  • you should rethink the overall approach since apparently you have huge issues sorting your data with the given data model. That typically is a sign of a modelling approach that should be reworked.
arkascha
  • 37,875
  • 7
  • 50
  • 85