添加具有相同键值的数组

时间:2017-02-22 12:34:57

标签: php arrays recursion merge

我希望添加一个数组,其中键值相同但只有一个键。

例如,我有一个打印出以下内容的数组:

Array
(
[0] => Array
    (
        [Booked Date] => 2017-02-12
        [salesMadeToday] => 128327.34
    )

[1] => Array
    (
        [Booked Date] => 2017-02-13
        [salesMadeToday] => 162808.03
    )

[2] => Array
    (
        [Booked Date] => 2017-02-14
        [salesMadeToday] => 84753.11
    )

[3] => Array
    (
        [Booked Date] => 2017-02-15
        [salesMadeToday] => 164110.29
    )

[4] => Array
    (
        [Booked Date] => 2017-02-16
        [salesMadeToday] => 106904.50
    )

[5] => Array
    (
        [Booked Date] => 2017-02-17
        [salesMadeToday] => 140185.52
    )

[6] => Array
    (
        [Booked Date] => 2017-02-18
        [salesMadeToday] => 58404.60
    )

[7] => Array
    (
        [Booked Date] => 2017-02-12
        [salesMadeToday] => 21979.00
    )

[8] => Array
    (
        [Booked Date] => 2017-02-13
        [salesMadeToday] => 6512.00
    )

[9] => Array
    (
        [Booked Date] => 2017-02-14
        [salesMadeToday] => 4534.00
    )

[10] => Array
    (
        [Booked Date] => 2017-02-17
        [salesMadeToday] => 14332.02
    )

[11] => Array
    (
        [Booked Date] => 2017-02-18
        [salesMadeToday] => 4904.00
    )

)

现在你可以看到一些日期是一样的。

我如何制作它以便合并键并添加salesMadeToday值?

调用数组:

$ liveForecastSales,这是通过使用array_merge_recursive()函数添加2个其他数组而创建的。

原始数组是通过从其他函数调用来计算的,这些函数从数据库中提取数据,因此在此处粘贴的代码太多了,但如果您需要任何其他信息,请询问。

谢谢

2 个答案:

答案 0 :(得分:0)

你试试这个简单的比较:

$arr = Array
(
[0] => Array
    (
        [Booked Date] => 2017-02-12
        [salesMadeToday] => 128327.34
    )

[1] => Array
    (
        [Booked Date] => 2017-02-13
        [salesMadeToday] => 162808.03
    )

[2] => Array
    (
        [Booked Date] => 2017-02-14
        [salesMadeToday] => 84753.11
    )

[3] => Array
    (
        [Booked Date] => 2017-02-15
        [salesMadeToday] => 164110.29
    )

[4] => Array
    (
        [Booked Date] => 2017-02-16
        [salesMadeToday] => 106904.50
    )

[5] => Array
    (
        [Booked Date] => 2017-02-17
        [salesMadeToday] => 140185.52
    )

[6] => Array
    (
        [Booked Date] => 2017-02-18
        [salesMadeToday] => 58404.60
    )

[7] => Array
    (
        [Booked Date] => 2017-02-12
        [salesMadeToday] => 21979.00
    )

[8] => Array
    (
        [Booked Date] => 2017-02-13
        [salesMadeToday] => 6512.00
    )

[9] => Array
    (
        [Booked Date] => 2017-02-14
        [salesMadeToday] => 4534.00
    )

[10] => Array
    (
        [Booked Date] => 2017-02-17
        [salesMadeToday] => 14332.02
    )

[11] => Array
    (
        [Booked Date] => 2017-02-18
        [salesMadeToday] => 4904.00
    )

);
function exists($date){
    for($i=0; $i<count($arr);$i++){

    if($arr[$i]['Booked Date']==$date){
        return true;
    }
    return false;
}
for($i=0; $i<count($arr);$i++){
    if(!exists($arr[$i]['Booked Date'])){
        $new[]=$arr[$i];
    }

}

答案 1 :(得分:0)

使用常规foreach循环和array_values函数的解决方案:

$result = [];
foreach ($liveForecastSales as $item) {
    if (!isset($result[$item['Booked Date']])) {
        $result[$item['Booked Date']] = $item;
    } else {
        $result[$item['Booked Date']]['salesMadeToday'] += $item['salesMadeToday'];
    }
}

print_r(array_values($result));

DEMO link