1

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
   )

  )
chris
  • 2,913
  • 4
  • 43
  • 48
  • you loop, if is_array(value) loop again – Ibu May 08 '14 at 20:05
  • It's a little trickier @Ibu since the item he's seeking is an Array also – willoller May 08 '14 at 20:05
  • 1
    Do you have the code of this recursive function you're mentioning? Because the way I see it, that is the problem. – Ozmah May 08 '14 at 20:10
  • @nl-x array_walk doesn't recurse. And array_walk_recursive will recurse too far, because it will go into the arrays at the leaves of his tree. – Barmar May 08 '14 at 20:16
  • @Ozmah I will post the recursive function as a separate question. Barmar has solved this one. – chris May 08 '14 at 20:36
  • @Ozmah [Here](http://stackoverflow.com/questions/23552616/recusive-function-outputting-a-wild-multidimentional-array-would-like-it-one-le?noredirect=1#comment36137574_23552616) is a link to the recursive function causing the issue. – chris May 08 '14 at 21:47
  • This is an inappropriately closed page, the dupe page provides 1-dim arrays, not 2-dim arrays as this question requires. – mickmackusa Sep 27 '19 at 13:49

1 Answers1

3
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);
Barmar
  • 741,623
  • 53
  • 500
  • 612