HareaCostea
HareaCostea

Reputation: 145

Calculate some totals based on existing arrays

I want to calculate some totals based on existing multidimensional arrays

My array :

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;
}

The idea is to build anew array based on the previous array + calculate totals for each GEOGRAFIE and push it in the array. So calculate sum of all columns expect "ORIGINE" and "GEOGRAPHIE" and put all values just after the array start with new "GEOGRPAPHIE". Something like this :

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
        )
)

Upvotes: 2

Views: 47

Answers (1)

Rahul
Rahul

Reputation: 18557

Please find my inline documentation for explanation,

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;
}

Working demo.

Upvotes: 1

Related Questions