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 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!