Hoàng Nguyễn
Hoàng Nguyễn

Reputation: 27

PHP array add Incremental

I have an array, i get from database

*

Array
(
    [0] => Array
        (
            [year] => "2016"
            [numb] => 1
        )
    [1] => Array
        (
            [year] => "2016"
            [numb] => 3
        )
    [2] => Array
        (
            [year] => "2017"
            [numb] => 3
        )
    [3] => Array
        (
            [year] => "2016"
            [numb] => 1
        )
    [4] => Array
        (
            [year] => "2018"
            [numb] => 2
        )
)

*

I want the result array will be like this

Array
(
    [2016] => 5
    [2017] => 8
    [2018] => 10
)

This means: 2016 = 1 +3 + 1 = 5, 2017 = (year2016) + 3, 2018 = (year2016) + (year2017) + 2. But 2016 is not always the first year, it depends on the database

Upvotes: 0

Views: 132

Answers (4)

Taha Paksu
Taha Paksu

Reputation: 15616

First method: use database query (the better and recommended way)

SELECT DISTINCT y1.year,
(SELECT sum(y2.numb) FROM nums y2 WHERE y2.year <= y1.year) AS numb_total
FROM nums y1

Fiddle: http://sqlfiddle.com/#!9/0c6ec5/12

Second method: use PHP methods

// create a container for the new array
$newArray = [];

// get the unique years from array
$years = array_unique(array_column($array, "year"));

// loop them
foreach($years as $year){

    // filter the array to match the years less or equal of the current year
    $year_filtered = array_filter($array, function($d)use($year){ return intval($d["year"]) <= intval($year);});

    // sum the numbers
    $total = array_sum(array_column($year_filtered, "numb"));

    // place the values in the new array
    $newArray[$year] = $total;
}

// echo the output (for debugging purposes)
print_r($newArray);

Fiddle: https://3v4l.org/UK9A8

Upvotes: 4

Andreas
Andreas

Reputation: 23958

I create a new array with key as year and value as numb.
Then sort the array on keys (year).
Then I sum the values in another loop.

There may be an easier way but this is what I could think of.

// create new array with year as key and numb as value
$res =array();
Foreach($arr as $val){
    If(!isset($res[$val["year"]])) $res[$val["year"]] =0;
    $res[$val["year"]] += $val["numb"];

}

ksort($res); // sort the array on keys to make earliest year first
$newarr= array();
$sum =0;
foreach($res as $key => $val){
    $newarr[$key] = $val+$sum; // new array with summed values from previous year(s)
    $sum +=$val;
}
Var_dump($newarr);

https://3v4l.org/5Ql2U

Upvotes: 0

Bharti
Bharti

Reputation: 1

$dbResult = array(array('year' => '2016', 'numb' => 1),  
                array('year' => '2016', 'numb' => 3), 
                array('year' => '2017', 'numb' => 3), 
                array('year' => '2016', 'numb' => 1), 
                array('year' => '2018', 'numb' => 2)); 

$result = array();
foreach ($dbResult as $data) {
    $result[$data['year']] = (isset($result[$data['year']])) ? $result[$data['year']] : 0;
    $result[$data['year']] = $result[$data['year']] + $data['numb'] ;
}
echo '<pre>';print_r($result);

Upvotes: -1

Umesh Maliya
Umesh Maliya

Reputation: 145

$MyNewArray = array();
foreach($YourArray as $Row){
    $MyNewArray[$Row->year] = $MyNewArray[$Row->year]+$Row->numb;
}
print_r($MyNewArray);

Upvotes: 2

Related Questions