0

I am stuck on something that might be very simple.

I am creating a new array by looping through an existing array using a recursion function yet I can not seem to get the values to stick to the new array. The function, in the end, will be a bit more complex, but for now I need some help.

I have tried soooo many ways to get this to work but I am at a loss right now.

Here is my php function

function recursive($array) {

    $newArray = array();

    foreach($array as $key => $value) {

        if(is_array($value)){
            recursive($value);                              
        }else{
            $newArray[] = $value;
        }   

    }

    return $newArray;

}

As is, the new array never gets filled...BUT, if I change this line...

recursive($value); // Why can't I just call the recursive function here?

...to...

$newArray[] = recursive($value); // Instead of having to set a new value to the new array?

everything works properly...except that my goal was to create a flat array with only the values.

So my question is, why is it necessary to set a new array value in order to call the recursive function again? Ideally, I want to skip setting a new array value if the value is an array and just continue the loop through the original array.

3
  • You might want to read up on array merge. Commented Aug 24, 2016 at 17:22
  • I guess I should've made that an answer instead of a comment Commented Aug 24, 2016 at 17:26
  • Thank you all for the great answers! Commented Aug 24, 2016 at 17:35

3 Answers 3

2

Use array_merge:

function recursive($array) {

    $newArray = array();

    foreach($array as $key => $value) {

        if(is_array($value)){
            $newArray = array_merge($newArray, recursive($value));                              
        }else{
            $newArray[] = $value;
        }   

    }

    return $newArray;

}

...or you could use special operator:

function recursive($array) {

    $newArray = array();

    foreach($array as $key => $value) {

        if(is_array($value)){
            $newArray += recursive($value);                              
        }else{
            $newArray[] = $value;
        }   

    }

    return $newArray;

}

...or pass a variable by reference like this:

function recursive($array, &$newArray = null) {

    if (!$newArray) {
        $newArray = array();
    }

    foreach($array as $key => $value) {

        if(is_array($value)){
            recursive($value, $newArray);                              
        }else{
            $newArray[] = $value;
        }   

    }

    return $newArray;   
}
Sign up to request clarification or add additional context in comments.

2 Comments

Can I ask you a related question? When the $newArray gets returned each cycle, is it always the sum of all the other cycles? I am just confused a bit on what is actually happening when the return is made.
Yes it is always the sum of all other cycles. Did you read about array_merge php.net/array_merge ?
1

use array_merge() to merge the array returned from recursive($value); and $newArray

$newArray = array_merge($newArray,recursive($value));

You can guarantee that $newArray will be flat after this, as the previous value of $newArray was flat, and recursive always returns a flat array, so the combination of both should be a flat array.

Comments

1

You aren't doing anything with the return from your recursive function. Try this:

function recursive($array) {

    $newArray = array();

    foreach($array as $key => $value) {

        if(is_array($value)){
            // This is what was modified
            $newArray = array_merge($newArray, recursive($value));                              
        }else{
            $newArray[] = $value;
        }   

    }

    return $newArray;

}

Comments

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.