-2

I have this array:

Array
(
    [one] => Array
        (
            [a] => 0
            [b] => 1
            [c] => 1
            [d] => 3
            [e] => 1
        )

    [two] => Array
        (
            [a] => 0
            [b] => 3
            [c] => 1
            [d] => 4
            [e] => 1
        )

    [three] => Array
        (
            [a] => 3
            [b] => 1
            [c] => 2
            [d] => 4
            [e] => 1
        )
)

And I want to convert it into single array with the values are the sums of every value inside the inner array, so it could be like this:

Array
        (
            [a] => 3
            [b] => 5
            [c] => 4
            [d] => 11
            [e] => 3
        )

How to achieve it?

EDIT

This was the best what I've done:

$rest = array();
foreach($result as $key => $value){
    if(is_array($value)) {
        foreach($value as $k => $val){
            $rest[$k] = array_sum($value);
        }
    }
}

But it returns all values to be the same, i.e all 9 on every inner key.

Ari
  • 4,643
  • 5
  • 36
  • 52

4 Answers4

2

You can get the keys from the first child array with array_keys, and then use array_sum and array_column to generate the array of sums.

foreach (array_keys($your_array['one']) as $key) {
    $sums[$key] = array_sum(array_column($your_array, $key));
}

array_column does require php >= 5.5.

Incidentally, what you already had was really close to working. If you change

$rest[$k] = array_sum($value);

to

$rest[$k] += $val;

It should be good to go. What you had before was repeatedly summing the entire sub-array and assigning it to each letter key, but you just needed to add the current value to that letter key.

$rest[$k] += $val; will work, but give you undefined index notices for the first sub-array. You can fix that by checking isset before assigning, like this:

$rest[$k] = isset($rest[$k]) ? $rest[$k] + $val : $val;

I would say modifying your original code to work this way would probably be better than redefining array_column if you can't use it.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

You have multidimensional Array , that why have to use two loop for understanding in beginning level. First loop will get Every Object of Array , Second Array will get the Params of Object.

$finalArray = array();
for($i = 0;$i<count($array);$i++){  // GET ALL OBJECT FROM ARRAY
foreach($array[$i] as $key=>$value){ // GET ALL KEY FROM OBJECT
 $finalArray[$key] += $array[$i][$key];
}
}
print_r($finalArray);

 Work on all version of PHP
Muhammad Ali
  • 1,992
  • 1
  • 13
  • 20
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Rizier123 Apr 05 '16 at 17:44
-1

You can use array_sum and loop over the first dimension

foreach ($array as $key => $values) {
  $newArray[$key] = array_sum($values);
}
-1

Credit to Don't Panic's answer above and this answer which is providing array_column() alternative for PHP version < 5.5

// if array_column function don't exist, add array_column function
if (! function_exists('array_column')) {
    function array_column(array $input, $columnKey, $indexKey = null) {
        $array = array();
        foreach ($input as $value) {
            if ( ! isset($value[$columnKey])) {
                trigger_error("Key \"$columnKey\" does not exist in array");
                return false;
            }
            if (is_null($indexKey)) {
                $array[] = $value[$columnKey];
            }
            else {
                if ( ! isset($value[$indexKey])) {
                    trigger_error("Key \"$indexKey\" does not exist in array");
                    return false;
                }
                if ( ! is_scalar($value[$indexKey])) {
                    trigger_error("Key \"$indexKey\" does not contain scalar value");
                    return false;
                }
                $array[$value[$indexKey]] = $value[$columnKey];
            }
        }
        return $array;
    }
}

// sum up the values
    $rest = array();
    foreach($result as $key => $arr){
        if(is_array($arr)) {
            foreach($arr as $k => $val){
                $rest[$k] = array_sum(array_column($result, $k));               
            }
        }
    }
Community
  • 1
  • 1
Ari
  • 4,643
  • 5
  • 36
  • 52