0

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

3
  • I have no solution to do this Commented Oct 24, 2017 at 7:03
  • Please read about array and learn how to iterate them. That should be enough to solve the problem. Commented Oct 24, 2017 at 7:03
  • you might want to change your query to something which uses group by and sum methods Commented Oct 24, 2017 at 7:05

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't either create the output wanted by OP.
@Andreas fixed both.
2
$MyNewArray = array();
foreach($YourArray as $Row){
    $MyNewArray[$Row->year] = $MyNewArray[$Row->year]+$Row->numb;
}
print_r($MyNewArray);

1 Comment

This does not create the output wanted by OP.
0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.