我想从多个数组中获得相同键的总和

时间:2014-04-16 13:24:47

标签: php arrays merge

第一阵列:

Array
(
    [0] => Array
        (
            [sales] => Array
                (
                    [0] => Array
                        (
                            [Date] => 2014-04-01
                            [Units] => 1
                        )

                    [1] => Array
                        (
                            [Date] => 2014-04-02
                            [Units] => 7
                        )


)

2nd Array ::

Array
(
    [0] => Array
        (
            [sales] => Array
                (
                    [0] => Array
                        (
                            [Date] => 2014-04-01
                            [Units] => 3
                        )

                    [1] => Array
                        (
                            [Date] => 2014-04-02
                            [Units] => 2
                        )


)

3rd Array ::

Array
(
    [0] => Array
        (
            [sales] => Array
                (
                    [0] => Array
                        (
                            [Date] => 2014-04-01
                            [Units] => 0
                        )

                    [1] => Array
                        (
                            [Date] => 2014-04-02
                            [Units] => 5
                        )


)

期望输出::

Array
(                     [0] => Array
                        (
                            [Date] => 2014-04-01
                            [Units] => 4
                        ),
                       [1] => Array
                        (
                            [Date] => 2014-04-02
                            [Units] => 14
                        ),
)

3 个答案:

答案 0 :(得分:1)

<?php
$array1 = array("orange", "banana");
$array2 = array("ab", "ba");
$array3 = array("cd", "ef");
array_push($array1,$array2 , $array3);
print_r($array1);
?>

答案 1 :(得分:0)

<?php

$arr1=array(
'Date' => '2014-04-02'
 'Units' => 5
    );
$arr2=array(
'Date' => '2014-04-02'
 'Units' => 5
    );
$arr3=array(
'Date' => '2014-04-02'
 'Units' => 5
    );

$arr['sales'][0]=$arr1;
  $arr['sales'][1]=$arr2;
  $arr['sales'][2]=$arr3;
$data[]=$arr;

print_r($data);
?>

答案 2 :(得分:0)

假设您提到了数组,可以采用以下方法:

<?php 

$first =  array ();
$first[0] =  array ( "Date" => "2014-04-01",  "Units" => 1 );
$first[1] =  array ( "Date" => "2014-04-02",  "Units" => 7 );
$first[2] =  array ( "Date" => "2014-04-03",  "Units" => 5 );
$first[3] =  array ( "Date" => "2014-04-04",  "Units" => 1 );



echo "<pre>";
print_r ($first);
echo "</pre>";
echo "<hr>";

$second =  array ();
$second[0] =  array ( "Date" => "2014-04-01",  "Units" => 3 );
$second[1] =  array ( "Date" => "2014-04-02",  "Units" => 2 );
$second[2] =  array ( "Date" => "2014-04-03",  "Units" => 5 );
$second[3] =  array ( "Date" => "2014-04-04",  "Units" => 2 );

echo "<pre>";
print_r ($second);
echo "</pre>";
echo "<hr>";

$third =  array ();
$third[0] =  array ( "Date" => "2014-04-01",  "Units" => 0 );
$third[1] =  array ( "Date" => "2014-04-02",  "Units" => 5 );
$third[2] =  array ( "Date" => "2014-04-03",  "Units" => 5 );
$third[3] =  array ( "Date" => "2014-04-04",  "Units" => 4 );

echo "<pre>";
print_r ($third);
echo "</pre>";
echo "<hr>The total is: ";



$total = array();

foreach ($first as $key=>$day) {
    $total[$key]=array("Date" => "2014-04-02",  "Units" => ($first[$key]["Units"]  +$second[$key]["Units"]  +$third[$key]["Units"]) );

}

echo "<pre>";
print_r ($total);
echo "</pre>";
echo "<hr>";

但这只适用于每个日期的密钥相同的情况。 (Aka,0始终对应于2014-04-01)。 如果我不得不使用它,我可能会看一步,如何创建这些数组,我会使用日期,作为关键