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
   )

  )
7
  • you loop, if is_array(value) loop again Commented May 8, 2014 at 20:05
  • It's a little trickier @Ibu since the item he's seeking is an Array also Commented May 8, 2014 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. Commented May 8, 2014 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. Commented May 8, 2014 at 20:16
  • @Ozmah I will post the recursive function as a separate question. Barmar has solved this one. Commented May 8, 2014 at 20:36

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

else should be elseif i guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.