VIDesignz
VIDesignz

Reputation: 4783

How to "skip" iteration when building a new array using recursion

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.

Upvotes: 0

Views: 352

Answers (3)

ofca
ofca

Reputation: 661

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;   
}

Upvotes: 2

nick.graziano
nick.graziano

Reputation: 678

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;

}

Upvotes: 1

Joseph Young
Joseph Young

Reputation: 2795

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.

Upvotes: 1

Related Questions