简体   繁体   English

如何在PHP中减去多维数组中的所有列值?

[英]How to subtract all column values in multi-dimensional array in Php?

How can I subtract all the columns values? 如何减去所有列的值? My array is like 我的数组就像

Array
(
    [0] => Array
        (
            [id] => 1
            [web_traffic] => 442
            [form_users] => 131
            [date] => 20181004
        )

    [1] => Array
        (
            [id] => 2
            [web_traffic] => 102
            [form_users] => 15
            [date] => 20181003
        )
    [2] => Array
        (
            [id] => 3
            [web_traffic] => 387
            [form_users] => 97
            [date] => 20181002
        )
)

I need to subtract the each column(except date & id) and get the result based on date(Ascending order). 我需要减去每一列(日期和ID除外),并根据日期(升序)获得结果。 For example 20181004 means 4th October 2018. My output should like the below 例如,20181004表示2018年10月4日。我的输出应如下所示

Array
(
    [web_traffic] => -152
    [form_users] => -49
)

My code took reference from How to sum all column values in multi-dimensional array? 我的代码参考了如何对多维数组中的所有列值求和?

foreach ($data as $value) {
        unset($value[ 'id' ]);
        $time = date('Ymd', strtotime($value[ 'date' ]));
        if (in_array($time, $dates)) {
            $value[ 'date' ] = $time;
            foreach ($value as $key => $secondValue) {
                if ( !isset($output[ $key ])) {
                    $output[ $key ] = 0;
                }
                $output[ $key ] -= $secondValue;
            }
        }
    }

Use PHP array_reduce() and array_column() like this: 像这样使用PHP array_reduce()array_column()

$initial_array = array(array('id'           => 1,
                     'web_traffic'  => 442,
                     'form_users'   => 131,
                     'date'         => 20181004),
               array('id'           => 2,
                     'web_traffic'  => 102,
                     'form_users'   => 15,
                     'date'         => 20181003),
               array('id'           => 3,
                     'web_traffic'  => 387,
                     'form_users'   => 97,
                     'date'         => 20181002));

function sum($carry, $item)
    {
        $carry -= $item;
        return $carry;
    }

$web_traffic  = array_column($initial_array, "web_traffic");
$form_users = array_column($initial_array, "form_users");
$date = array_column($initial_array, "date");


array_multisort($date, SORT_ASC, $form_users, SORT_DESC,  $initial_array);


$result_array = Array(
            "web_traffic" => array_reduce(array_column($initial_array, "web_traffic"), "sum",2*$initial_array[0]['web_traffic']),
            "form_users" => array_reduce(array_column($initial_array, "form_users"), "sum",2*$initial_array[0]['form_users'])
        );

print_r($result_array);

I would first sort the array using usort() in example. 我将首先在示例中使用usort()对数组进行排序。

Sorting first, because that can be tricky to substract in 1 loop the oldest datas to the newers one. 首先进行排序,因为将1减去就很难将最旧的数据循环到较新的数据。

Separating intentions provide a cleaner code, easier to maintain. 分开的意图提供了更简洁的代码,更易于维护。

The dates don't need to be converted into a date . 日期不需要转换为date The string is well formatted and can be used as is in a comparison and a sort. 该字符串格式正确,可以在比较和排序中按原样使用。 "20170101" < "20180101" , "20180101" < "20181001" and "20181002" < "20181004" "20170101" < "20180101""20180101" < "20181001""20181002" < "20181004"

When done, I'll save the first values as initial values to be used in substract. 完成后,我将把第一个值保存为要在减法中使用的初始值。

Then, loop the sorted array and substract the 'web_traffic' and 'form_users' to the initial values. 然后,循环排序数组,并将'web_traffic''form_users'初始值。

$datas = array(array('id'           => 1,
                     'web_traffic'  => 442,
                     'form_users'   => 131,
                     'date'         => 20181004),
               array('id'           => 2,
                     'web_traffic'  => 102,
                     'form_users'   => 15,
                     'date'         => 20181003),
               array('id'           => 3,
                     'web_traffic'  => 387,
                     'form_users'   => 97,
                     'date'         => 20181002));

//If needed, you can backup the original array, because usort() will modify it.
$backUpDatas = $datas;

//Sort
usort($datas, function ($arr1, $arr2)
{
    if (isset($arr1['date']) && isset($arr2['date']))
    {
        if ($arr1['date'] == $arr2['date'])
            return (0);
        return (($arr1['id'] > $arr2['id']) ? (-1) : (1));
    }
});

//Initial values
$finalValues['web_traffic'] = $datas[0]['web_traffic'];
$finalValues['form_users'] = $datas[0]['form_users'];

//Substract
foreach ($datas as $key => $value)
{
    if ($key > 0)
    {
        if (isset($value['web_traffic']))
            $finalValues['web_traffic'] -= $value['web_traffic'];
        if (isset($value['form_users']))
            $finalValues['form_users'] -= $value['form_users'];
    }
}

var_dump($finalValues);

Output : 输出:

array (size=2)
  'web_traffic' => int -157
  'form_users' => int -49

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM