-2

I am trying to change the following array to an almost flat array. So id 4 would be in the first level of the array, as would id 6 and 5, but still have their own index so I can tell which page is which. But with the same order as they have now. I presume that the solution would be some sort of recursive PHP function but I haven't a clue how to do this.

Array
(
[0] => Array
    (
        [id] => 2
        [identifier] => External URL
        [parent] => 0
        [sortOrder] => 1
        [depth] => 0
    )

[1] => Array
    (
        [id] => 3
        [identifier] => First Team
        [parent] => 0
        [sortOrder] => 2
        [depth] => 0
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 4
                        [identifier] => League tables
                        [parent] => 3
                        [sortOrder] => 0
                        [depth] => 1
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 6
                                        [identifier] => British and Irish Cup Tables
                                        [parent] => 4
                                        [sortOrder] => 24
                                        [depth] => 2
                                    )

                                [1] => Array
                                    (
                                        [id] => 5
                                        [identifier] => Greene King IPA Championship
                                        [parent] => 4
                                        [sortOrder] => 25
                                        [depth] => 2
                                    )

                            )

                    )

            )

    )

[2] => Array
    (
        [id] => 1
        [identifier] => Home
        [parent] => 0
        [sortOrder] => 25
        [depth] => 0
    )

)
Scott Bowers
  • 175
  • 3
  • 13
  • 1
    [Did you Google your question title?](https://www.google.co.uk/search?q=Flatten+multidimensional+array+recursively+php&oq=Flatten+multidimensional+array+recursively+php&gs_l=psy-ab.3...2538.2982.0.4106.4.4.0.0.0.0.187.403.3j1.4.0....0...1.1.64.psy-ab..0.3.214...33i22i29i30k1.0.JegtayTjrNU) – Script47 Sep 19 '17 at 13:56
  • 4
    Possible duplicate of [How to "flatten" a multi-dimensional array to simple one in PHP?](https://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php) – Script47 Sep 19 '17 at 13:56
  • Unfortunately this solution does not solve my issue. I have tried this code before and it only shows me the first index in the array. – Scott Bowers Sep 19 '17 at 14:00
  • You tried *all* the answers? – Script47 Sep 19 '17 at 14:05
  • I have yes. The problem I have is that I still need them to keep an initial index. So rather than them being completely flat, I need them to still be different from each other. – Scott Bowers Sep 19 '17 at 14:40
  • 1
    Can you post what the expected output is and what you've tried so far? it would help us help you a lot – William Perron Sep 19 '17 at 15:06

2 Answers2

0
<?php

$data = [
    [
        'id' => 1,
        'name' => 'one',
        'children' =>
        [
            [
                'id' => 2,
                'name' => 'two',
                'children' =>
                [
                    [
                        'id' => 4,
                        'name' => 'four'
                    ]
                ]
            ],
            [
                'id' => 3,
                'name' => 'three',
                'children' =>
                 [
                    [
                        'id' => 5,
                        'name' => 'five'                        
                    ]
                ]
            ]
        ]
    ],
    [
        'id' => 6,
        'name' => 'six'
    ]   
];

$stanley = [];
$flatten = function(array $data) use (&$flatten, &$stanley) {
    foreach($data as $k => $v) {
        if(isset($v['children'])) {
            $flatten($v['children']);
            unset($v['children']);
        }
        $stanley[] = $v;
    }
};

$flatten($data);
var_export($stanley);

Output:

array (
  0 => 
  array (
    'id' => 4,
    'name' => 'four',
  ),
  1 => 
  array (
    'id' => 2,
    'name' => 'two',
  ),
  2 => 
  array (
    'id' => 5,
    'name' => 'five',
  ),
  3 => 
  array (
    'id' => 3,
    'name' => 'three',
  ),
  4 => 
  array (
    'id' => 1,
    'name' => 'one',
  ),
  5 => 
  array (
    'id' => 6,
    'name' => 'six',
  ),
)
Progrock
  • 7,373
  • 1
  • 19
  • 25
0

I have found the solution! I built a recursive PHP function which utilised the depth index to track which level each of the items are while still keeping the array flat (ish).

function dropdownNavigationTree($array) {

$response = [];

   foreach($array as $page) {

       if (!is_array($page['children'])) { 

           $response[$page['id']] = ($page['depth'] > 0 ? str_repeat("-", $page['depth']).' ' : FALSE).$page['identifier'];

       } else {

           $response[$page['id']] = ($page['depth'] > 0 ? str_repeat("-", $page['depth']).' ' : FALSE).$page['identifier'];
           $children = dropdownNavigationTree($page['children']);

           $response = $response + $children;

       }

   }

   return $response;

}
Scott Bowers
  • 175
  • 3
  • 13