根据现有数组计算一些总数

时间:2019-05-13 09:06:43

标签: php arrays multidimensional-array php-7.1

我想基于现有的多维数组计算一些总数

我的数组:

Array
(
    [0] => Array
        (
            [ORIGINE] => AL
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => QUINZE
        )
    [1] => Array
        (
            [ORIGINE] => SU
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => QUINZE
        )
    [2] => Array
        (
            [ORIGINE] => RE
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => MEMBRES
        )

    [3] => Array
        (
            [ORIGINE] => SL
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => MEMBRES
        )

    [4] => Array
        (
            [ORIGINE] => BE
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => EST
        )

    [5] => Array
        (
            [ORIGINE] => RP
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => EST
        )

    [6] => Array
        (
            [ORIGINE] => DF
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => TIERS
        )

    [7] => Array
        (
            [ORIGINE] => AR
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => TIERS
        )
)


I tried something like this :
public function calculateTotals($datas)
{
    $totals = [];
    foreach ($datas as $data) {
        $nextElement = next($data);
        foreach ($data as $key => $value) {
            if($data['GEOGRAPHIE'] == $nextElement['GEOGRAPHIE']) {
                if(!in_array($key, ['ORIGINE', 'GEOGRAPHIE'])) {
                    $totals[$key] += $value;
                }
            } else {

            }
        }
    }

    return $datas;
}

这个想法是基于先前的数组构建一个新的数组+计算每个GEOGRAFIE的总数并将其推入数组。因此,计算期望“ ORIGINE”和“ GEOGRAPHIE”的所有列的总和,并在数组以新的“ GEOGRPAPHIE”开始之后放置所有值。像这样:

Array
(
    [0] => Array
        (
            [ORIGINE] => AL
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => QUINZE
        )
    [1] => Array
        (
            [ORIGINE] => SU
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => QUINZE
        )
    [2] => Array
        (
            [ORIGINE] => QUINZE
            [t] => 2
            [p] => 4
            [e] => 6
            [f] => 8
            [GEOGRAPHIE] => QUINZE
        )
    [3] => Array
        (
            [ORIGINE] => RE
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => MEMBRES
        )

    [4] => Array
        (
            [ORIGINE] => SL
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => MEMBRES
        )

    [5] => Array
        (
            [ORIGINE] => MEMBRES
            [t] => 2
            [p] => 4
            [e] => 6
            [f] => 8
            [GEOGRAPHIE] => MEMBRES
        )

    [5] => Array
        (
            [ORIGINE] => BE
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => EST
        )

    [6] => Array
        (
            [ORIGINE] => RP
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => EST
        )
    [7] => Array
        (
            [ORIGINE] => EST
            [t] => 2
            [p] => 4
            [e] => 6
            [f] => 8
            [GEOGRAPHIE] => EST
        )

    [8] => Array
        (
            [ORIGINE] => DF
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => TIERS
        )

    [9] => Array
        (
            [ORIGINE] => AR
            [t] => 1
            [p] => 2
            [e] => 3
            [f] => 4
            [GEOGRAPHIE] => TIERS
        )
    [10] => Array
        (
            [ORIGINE] => TIERS
            [t] => 2
            [p] => 4
            [e] => 6
            [f] => 8
            [GEOGRAPHIE] => TIERS
        )
)

1 个答案:

答案 0 :(得分:2)

请找到我的嵌入式文档进行解释,

function calculateTotals($datas)
{
    $totals = [];
    $result = [];
    foreach ($datas as $key1 => $data) {
        $nextElement       = (!empty($datas[$key1 + 1]) ? $datas[$key1 + 1] : []); // checking for next element exists
        $result[]          = $data;
        $totals['ORIGINE'] = $data['GEOGRAPHIE']; // setting total array's ORIGINE and GEOGRAPHIE to merge
        foreach ($data as $key => $value) {
            if (!in_array($key, ['ORIGINE', 'GEOGRAPHIE'])) { // fetching only required keys
                $totals[$key] = (!empty($totals[$key]) ? $totals[$key] : 0);
                $totals[$key] += $value; // adding every other keys
            }
        }
        $totals['GEOGRAPHIE'] = $data['GEOGRAPHIE']; // setting total array's ORIGINE and GEOGRAPHIE to merge
        if (empty($nextElement) || $data['GEOGRAPHIE'] != $nextElement['GEOGRAPHIE']) { // checking if next is different then push
            array_push($result, $totals);
            $totals = []; // resetting total array
        }
    }
    return $result;
}

工作demo