0

我有

Array_A ( [0] => Array ( [DATE] => 2012-11-28 , [totalCount] => )
          [1] => Array ( [DATE] => 2012-11-29 , [totalCount] => )
          [2] => Array ( [DATE] => 2012-11-30 , [totalCount] => ) )

Array_B ( [10] => Array ( [DATE] => 2012-11-28 , [totalCount] => 30 )
          [11] => Array ( [DATE] => 2012-11-30 , [totalCount] => 40 )
          [12] => Array ( [DATE] => 2012-12-05 , [totalCount] => 50 ) )

如果我需要用 Array_B 中的值替换 Array_A 并且输出应该是

Array_A ( [0] => Array ( [DATE] => 2012-11-28 , [totalCount] => 30 )
          [1] => Array ( [DATE] => 2012-11-29 , [totalCount] =>    )
          [2] => Array ( [DATE] => 2012-11-30 , [totalCount] => 40 ) )
4

2 回答 2

3

我刚刚注意到你只希望使用密钥的部分array_a,这个循环会做。

foreach($array_a as $key => $value){
    if (array_key_exists($key, $array_b){
        $result[$key] = $array_b[$key]; 
    } else {
        $result[$key] = null;
    }
}

或者,如果您想要在不存在array_akey in 的情况下使用的 value,只需将-clause 中的语句替换为: 。array_belse$result[$key] = $value;

于 2012-12-27T08:47:41.093 回答
2

尝试

$result = array_merge_recursive($Array_A, $Array_B);
print_r($result);
于 2012-12-27T08:42:56.507 回答