Multi dimensional array of unknown depth to one dimensonal array and relevant keys in PHP

StackOverflow https://stackoverflow.com/questions/23551451

  •  18-07-2023
  •  | 
  •  

Question

I have a recursive function pumping out data that looks like this

Array
(
[17] => Array
    (
        [cat_id] => 17
        [cat_name] => test.example.1
        [cat_parent] => 16
        [cat_slug] => Test Example 1
    )

[18] => Array
    (
        [16] => Array
            (
                [cat_id] => 16
                [cat_name] => test.example.2
                [cat_parent] => 15
                [cat_slug] => Test Example 2
            )

        [17] => Array
            (
                [15] => Array
                    (
                        [cat_id] => 15
                        [cat_name] => test.example.3
                        [cat_parent] => 6
                        [cat_slug] => Test Example 3
                    )

                [16] => Array
                    (
                        [6] => Array
                            (
                                [cat_id] => 6
                                [cat_name] => test.example.4
                                [cat_parent] => 2
                                [cat_slug] => Test Example 4
                            )

                        [7] => Array
                            (
                                [2] => Array
                                    (
                                        [cat_id] => 2
                                        [cat_name] => test.example.5
                                        [cat_parent] => 0
                                        [cat_slug] => Test Example 5
                                    )

                            )

                    )

            )

    )

)

I cannot find a way to create this array in one dimensional so I am hoping someone can help make it one dimensional with some array play, keeping the relevant keys.

example.

 Array
 (
   [17] => Array
   (
    [cat_id] => 17
    [cat_name] => test.example.1
    [cat_parent] => 16
    [cat_slug] => Test Example 1
 )

    [16] => Array
 (
    [cat_id] => 16
    [cat_name] => test.example.2
    [cat_parent] => 15
    [cat_slug] => Test Example 2
  )


    [15] => Array
  (
    [cat_id] => 15
    [cat_name] => test.example.3
    [cat_parent] => 6
    [cat_slug] => Test Example 3
   )


    [6] => Array
   (
    [cat_id] => 6
    [cat_name] => test.example.4
    [cat_parent] => 2
    [cat_slug] => Test Example 4
   )


    [2] => Array
   (
    [cat_id] => 2
    [cat_name] => test.example.5
    [cat_parent] => 0
    [cat_slug] => Test Example 5
   )

  )
Was it helpful?

Solution

function flatten($array, &$newArray) {
    foreach($array as $i => $el) {
        if (!is_array($array)) {
            return;
        }
        if (isset($el['cat_id'])) {
            $newArray[$i] = $el;
        } elseif (is_array($el)) {
            flatten($el, $newArray);
        }
    }
}
$result = array();
flatten($array, $result);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top