开发者

PHP: Warning: array_merge_recursive(): recursion detected

开发者 https://www.devze.com 2022-12-18 17:08 出处:网络
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 开发者_StackOverflowinto this function:

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 开发者_StackOverflowinto 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!


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消