1

I have the following array structure:

Array
(
    [0] => Array
        (
            [id] => 83
            [parent_id] => 0
            [title] => Questionnaire one
        )

    [1] => Array
        (
            [id] => 84
            [parent_id] => 0
            [title] => Questionnaire two
        )

    [2] => Array
        (
            [id] => 85
            [parent_id] => 83
            [title] => Questionnaire three
        )

)

I want to re-structure the array so child items are listed under their parents. For example:

Array
(
    [0] => Array
        (
            [id] => 83
            [parent_id] => 0
            [title] => Questionnaire one
        )

    [1] => Array
        (
            [id] => 85
            [parent_id] => 83
            [title] => Questionnaire three
        )

    [2] => Array
        (
            [id] => 84
            [parent_id] => 0
            [title] => Questionnaire two
        )
)

I've searched previous questions but found none of them actually achieve the above.

Can someone please help me with this?

Thanks

RobV
  • 28,022
  • 11
  • 77
  • 119
Sean Delaney
  • 328
  • 6
  • 21

2 Answers2

2

You can try

$array = Array(
        "0" => Array("id" => 83,"parent_id" => 0,"title" => "Questionnaire one"),
        "1" => Array("id" => 84,"parent_id" => 0,"title" => "Questionnaire two"),
        "2" => Array("id" => 85,"parent_id" => 83,"title" => "Questionnaire three"));

$id = array_map(function ($item) {return $item["id"];}, $array);
$parent = array_filter($array, function ($item){return $item['parent_id'] == 0;});
$lists = array();

foreach ($parent as $value)
{
    $lists[] = $value ;
    $children = array_filter($array, function ($item) use($value) {return $item['parent_id'] == $value['id'];});
    foreach($children as $kids)
    {
        $lists[]  = $kids ;
    }
}

echo "<pre>";
print_r($lists);

Output

Array
(
    [0] => Array
        (
            [id] => 83
            [parent_id] => 0
            [title] => Questionnaire one
        )

    [1] => Array
        (
            [id] => 85
            [parent_id] => 83
            [title] => Questionnaire three
        )

    [2] => Array
        (
            [id] => 84
            [parent_id] => 0
            [title] => Questionnaire two
        )

)
Baba
  • 94,024
  • 28
  • 166
  • 217
  • `multiGroup($array, "id", "parent_id")` prints out the same array structure as my original array. I want the child items to be listed under their parents? I also don't want to create a child item with a sub array like your second example. – Sean Delaney Oct 18 '12 at 19:11
  • Does this solution require any specific PHP version as I get this output: `Array ( [0] => Array ( [id] => 83 [parent_id] => 0 [title] => Questionnaire one ) [1] => Array ( [id] => 84 [parent_id] => 0 [title] => Questionnaire two ) [2] => Array ( [id] => 85 [parent_id] => 83 [title] => Questionnaire three ) )` which is the same input. I'm running PHP 5.3.3. I have a feeling use() is not running for me. – Sean Delaney Oct 18 '12 at 19:38
  • Nope it works fine with php 5.3 .. see http://codepad.viper-7.com/hirHId what version are you using ??? – Baba Oct 18 '12 at 19:41
  • I'm running 5.3.3. Strange as I have copied and pasted your code and my output is not correct. Is the same solution possible without using use() function. For some reason this is not working for me? – Sean Delaney Oct 18 '12 at 19:49
  • Hold on then let me take a look at this again carefully – Baba Oct 18 '12 at 20:01
  • If I pass in child as 3rd param to multiGroup it works as expected. – Sean Delaney Oct 18 '12 at 20:09
  • http://codepad.viper-7.com/p16TvU I cant vote up as i'm not allowed but I want to say thank you so much for your help and patience with me. – Sean Delaney Oct 18 '12 at 20:10
1

You could use uksort(). Heres a DEMO.

function cmp($a, $b) {
  if ($stock[$a] != $stock[$b]) return $stock[$b] - $stock[$a];
  return strcmp($a, $b);
}

$a = array(5 => 'apple', 1 => 'banana', 6 => 'orange', 2 => 'kiwi');

uksort($a, "cmp");

foreach ($a as $key => $value) {
   echo "$key: $value\n";
}
Jonathan Eckman
  • 2,071
  • 3
  • 23
  • 49