简体   繁体   English

PHP:警告:array_merge_recursive():检测到递归

[英]PHP: Warning: array_merge_recursive(): recursion detected

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 警告:array_merge_recursive():在第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... 我只是不知道什么设计会导致这种情况发生...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM