Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 合并数组忽略空值?_Php_Arrays_Multidimensional Array_Foreach - Fatal编程技术网

Php 合并数组忽略空值?

Php 合并数组忽略空值?,php,arrays,multidimensional-array,foreach,Php,Arrays,Multidimensional Array,Foreach,我正在尝试将以下数组中的值提取到新数组中: 我收到了有关foreach命令的帮助,但现在在组合两个数组时遇到了问题,其中一个数组的值为null 原始阵列是: 使用以下命令: foreach ($arr['products']['product'] as $num) { $pid = $num['pid']; foreach($num['configoptions']['configoption']['0']['options']['option'] as $option)

我正在尝试将以下数组中的值提取到新数组中:

我收到了有关
foreach
命令的帮助,但现在在组合两个数组时遇到了问题,其中一个数组的值为
null

原始阵列是:

使用以下命令:

foreach ($arr['products']['product'] as $num) {

    $pid = $num['pid'];

    foreach($num['configoptions']['configoption']['0']['options']['option'] as $option)
        {
            $name = $option['name'];
            $yearlycosts = $option['pricing']['GBP']['monthly'];

            $namearr[] = $name;
            $yearlyarr[] = $yearlycosts;

            $peryear = array_combine($namearr, $yearlyarr);

        }

    $pricing[$pid] = array('cost' => $peryear);

}   

print_r($pricing);
我已经能够生成以下阵列:

Array
(
    [2] => Array
        (
            [cost] => Array
                (
                    [1 Year] => 4.17
                    [2 Years] => 8.33
                    [3 Years] => 12.50
                    [4 Years] => 16.67
                    [5 Years] => 20.83
                )

        )

    [3] => Array
        (
            [cost] => Array
                (
                    [1 Year] => 40.83
                    [2 Years] => 8.33   <---
                    [3 Years] => 12.50     |--- These values shouldn't be here!
                    [4 Years] => 16.67     |
                    [5 Years] => 20.83  <---
                )

        )

    [4] => Array
        (
            [cost] => Array
                (
                    [1 Year] => 49.17
                    [2 Years] => 79.17
                    [3 Years] => 115.83
                    [4 Years] => 149.17
                    [5 Years] => 190.83
                )

        )

)
数组
(
[2] =>阵列
(
[成本]=>阵列
(
[1年]=>4.17
[2年]=>8.33
[3年]=>12.50
[4年]=>16.67
[5年]=>20.83
)
)
[3] =>阵列
(
[成本]=>阵列
(
[1年]=>40.83
[2年]=>8.33 12.50 |----这些值不应该出现在这里!
[4年]=>16.67|
[5年]=>20.83阵列
(
[成本]=>阵列
(
[1年]=>49.17
[2年]=>79.17
[3年]=>115.83
[4年]=>149.17
[5年]=>190.83
)
)
)
问题是,如果查看原始数组,则没有产品ID的价格信息
[3]
。第2年到第5年已填充了产品ID
[2]
的第2年到第5年的值


我曾经尝试过
数组过滤器
取消设置
,但我不知道如何防止这种情况发生(或者为什么会发生).

您需要在内部循环之前清空数组,这样就不会保留主循环上一次迭代中的值。您应该在内部循环之外执行
数组\u组合

foreach ($arr['products']['product'] as $num) {

    $pid = $num['pid'];

    $namearr = array();
    $yearlyarr = array();

    foreach($num['configoptions']['configoption']['0']['options']['option'] as $option)
        {
            $name = $option['name'];
            $yearlycosts = $option['pricing']['GBP']['monthly'];

            $namearr[] = $name;
            $yearlyarr[] = $yearlycosts;

        }

    $peryear = array_combine($namearr, $yearlyarr);
    $pricing[$pid] = array('cost' => $peryear);

}   

print_r($pricing);

为什么每次都要通过循环执行
array\u combine
,而不是在循环完成时执行?现在看来,这显然是错误的。谢谢。这非常有效。谢谢。因为我是数组新手,所以我假设数组是空的,因为它们还没有定义。在定义之前它们是空的。但是在第一次通过在循环中,它们是被定义的,所以它们不再是空的。PHP中的变量不是特定循环的本地变量;PHP中的变量范围是按函数或类,而不是按更细粒度的块。我明白了。这帮助我更好地理解了为什么我的代码不能按预期工作。再次感谢您的帮助(和解释)。