PHP:我如何将2个不同的数组合并为一个

时间:2013-02-09 16:21:30

标签: php arrays

有没有办法将2个数组合并为一个数组?

我的第一个数组显示每天计算的金额。

$ArrayBefore[] = $amountOfTransactions_prDayArrayBefore;    

输出:

Array (
    [0] => Array (
        [09/02] => 102.83
        [08/02] => 3852.49
        [07/02] => 2619.23
        [06/02] => 1135.24
        [05/02] => 2391.4
        [04/02] => 2376.15
        [03/02] => 2760.05
        [02/02] => 1946.41
    )
) 

我的第二个数组显示每天的交易金额。

$ArrayTrans[] = $amountOfTransactions_prDayArrayTrans;

输出:

Array (
    [0] => Array (
        [09/02] => 2
        [08/02] => 30
        [07/02] => 30
        [06/02] => 15
        [05/02] => 36
        [04/02] => 31
        [03/02] => 28
        [02/02] => 14
    )
) 

有没有办法将它们组合成一个数组。我希望它们看起来像这样,所以我可以把它们吐在一张桌子里。

Array (
    [0] => Array (
        [09/02] => 102.83 => 2
        [08/02] => 3852.49 => 30
        [07/02] => 2619.23 => 30
        [06/02] => 1135.24 => 15
        [05/02] => 2391.4 => 36
        [04/02] => 2376.15 => 31
        [03/02] => 2760.05 => 28
        [02/02] => 1946.41 => 14
    )
) 

2 个答案:

答案 0 :(得分:0)

我调用第一个数组$ a,第二个数组$ b

foreach ( $a[0] as $key=>$value )
{
    $c[0]["$value"] = $b[0][$key];
}

   $c = array (size=1)
      0 => 
        array (size=8)
          '102.83' => int 2
          '3852.49' => int 30
          '2619.23' => int 30
          '1135.24' => int 15
          '2391.4' => int 36
          '2376.15' => int 31
          '2760.05' => int 28
          '1946.41' => int 14

答案 1 :(得分:-1)

使用array_merge()

$result = array_merge($ArrayBefore, $ArrayTrans);

编辑:

明白我的php是生锈的,但是如何处理以下内容:

foreach($ArrayBefore[] as $key => $value) {
    $tmpPair[$value] = $ArrayTrans[0][$key];
    $newArray[$key] = $tmpPair;
}

您也可以简化它

foreach($ArrayBefore[] as $key => $value) {
    $newArray[$key][$value] = $ArrayTrans[0][$key];
}