1

I have a list of arrays (unknown amount), I need to merge all of them recursively.

So what I did what create an array of all of those arrays and pass them into this function:

function mergeMonth($array)
{
    foreach($array as $date_string => $inner_array)
    {
        if(isset($temp_inner_array))
        {
            $temp_inner_array = array_merge_recursive($temp_inner_array,$inner_array);
        }
        else
        {
            $temp_inner_array = $inner_array;
        }
    }

    return $temp_inner_array;
}

Most of the time this works just like I expected it to, but sometimes I get this error:

Warning: array_merge_recursive(): recursion detected in ... on line 89

Don't know why?

Any ideas?

Thanks!!

UPDATE

the structure is like this:

Array
(
    [sales] => 301.5
    [cost] => 
    [repairs] => 0
    [esps] => 0
    [margin] => 301.5
    [verified] => unverified
)

Which I then changed to:

Array
(
    [sales] => 301.5
    [cost] => 0
    [repairs] => 0
    [esps] => 0
    [margin] => 301.5
    [verified] => unverified
)

and that fixed the issue :)

Note anyone who can explain WHY my change fixed it, will get the accepted answer!

1
  • From @PvB - "Without having infos about the arrays you put into the function it's quite hard to clear things out. Could you provide examples of the cases resulting in a warning? Or describe the structure of your "inner" arrays." Commented Jan 29, 2010 at 16:06

1 Answer 1

1

On possibility is that one array was referencing another one.

Simple example

        $a = array
        (
            'cost' => null,
        );
        $b = array
        (
            'cost' => &$a['cost'], // appears as "[cost] => " 
                                   // because $a['cost'] is null
        );

This results in an recursion.

I just don't know what design would cause that to happen...

Sign up to request clarification or add additional context in comments.

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.