douquan2023 2013-10-02 03:20
浏览 93
已采纳

ForEach期间的PHP合并数组

I am taking information from a JSON feed, and creating a JSON feed that I can use more easily across more devices. The problem I am having is when creating the new array in PHP for the JSON feed, I need to use a foreach loop. Here is my code for the foreach loop:

$obj = json_decode($json);
$json_decode = objectToArray($obj);

$special_number = '0';
$special_number2 = '0';
foreach($json_decode AS $r) {
$info = explode('.', $json_decode[$special_number]['Id']);
$newarray = array( $special_number => array( 'client' => $info['4'], 'placementID' => $info['2'], 'creativeID' => $info['3'], 'dimensions' => $info['3'], 'impressions' => $json_decode[$special_number]['Impressions'], 'bxd' => $json_decode[$special_number]['BXD'], 'viewable_impressions' => $json_decode[$special_number]['ViewableImpressions'], 'exposure' => $json_decode[$special_number]['Exposure'], 'viewability_rate' => $json_decode[$special_number]['ViewableRate'], 'clicks' => $json_decode[$special_number]['AdClicks'], 'mouse_overs' => $json_decode[$special_number]['AdMouseOvers'] ));

$fullarray = array_merge($newarray);

$special_number++;
}

I am basically taking their arrays, reordering the info and getting only the data I need, and creating a new array with it, called $fullarray. Each of the sub arrays I am making are generated each time, is it due to me running a foreach it is destorying the old $fullarray, and creating a new one? It is giving me the following:

Array
(
[0] => Array
    (
        [client] => 
        [placementID] => 3
        [creativeID] => 4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
        [dimensions] => 4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
        [impressions] => 1
        [bxd] => 0
        [viewable_impressions] => 0
        [exposure] => 0
        [viewability_rate] => 0
        [clicks] => 1
        [mouse_overs] => 1
    )

)

I have searched for this, but I can not find it, other threads are about combining different keys and values, I am having to create subarrays to keep the keys the same, and have different values (this is what I want, yes.)

Any help will be appreciated! :)

  • 写回答

1条回答 默认 最新

  • duanshang7007 2013-10-02 04:03
    关注

    This should do it:

    $fullarray = array()
    foreach(...) 
    {
        ...
        $fullarray = array_merge($fullarray,$newarray);
    }
    

    The merge function can take more arrays parameters to be merged, in your case you did not merged the contents of fullarray to the new array, instead you overwrite it.

    You may also need to declare full array before foreach

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。