dtw52353 2013-12-14 08:56
浏览 79
已采纳

合并数组忽略空值?

I'm trying to extract values from the following array into a new array:

I received help with the foreach command, but am now having trouble combining two arrays, one of which has null values.

The original array is: http://pastebin.com/jeKVxpka

Using the following:

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

I've been able to produce the following array:

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
                )

        )

)

The problem is, if you look at the original array, there is no price information for product ID [3]. Years 2 to 5 have been populated with the values from years 2 to 5 for product ID [2].

I've experimented with array_filter and unset, but I can't figure out how to prevent this from happening (or why it is happening).

  • 写回答

1条回答 默认 最新

  • drbd65446 2013-12-14 09:08
    关注

    You need to empty the arrays before the inner loops, so you don't keep the values from the previous iteration of the main loop. And you should do the array_combine outside the inner loop.

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条