0

I have a multidimensional array that looks like this..

$array = array (
    'section1' => array (
        'value'  => 465,
        'value2' => 744
    ),
    'section2' => array (
        'value'  => 6544,
        'value2' => 565
    ),
    'section5' => array (
        'value'  => 345,
        'value2' => 7465
    )
);

I want to add all of the value2 together. I know I can do it with a for loop but is there a way to do it without doing this?

Could I use array_sum in combination with array_column to do this? The number of sections and the section name changes so I am not sure how to cater for this.

3
  • Could I use array_sum in combination with array_column to do this? Yes Commented Dec 18, 2021 at 15:03
  • 2
    You can use array_reduce Commented Dec 18, 2021 at 15:04
  • echo array_sum(array_column( $array, 'value2')); Commented Dec 18, 2021 at 15:15

4 Answers 4

2

Use array_map to extract the required key's value, and then use array_sum.

$a = array (
    'section1' => array (
        'value'  => 465,
        'value2' => 744
    ),
    'section2' => array (
        'value'  => 6544,
        'value2' => 565
    ),
    'section5' => array (
        'value'  => 345,
        'value2' => 7465
    )
);

echo array_sum(array_map('v',$a));

function v($v) {return $v['value2'];}

Teh Playground!

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

Comments

2

array_reduce variant:

$a = array (
    'section1' => array (
        'value'  => 465,
        'value2' => 744
    ),
    'section2' => array (
        'value'  => 6544,
        'value2' => 565
    ),
    'section5' => array (
        'value'  => 345,
        'value2' => 7465
    )
);

print_r($a);

echo array_reduce($a, function($c, $s) { return $c + $s['value2']; }, 0);

// shorter with arrow functions
echo array_reduce($a, fn($c, $s) => $c + $s['value2'], 0);

Fiddle here.

Comments

1

You can use array_walk for example and passing $sum by reference.

<?php
$array = array (
    'section1' => array (
        'value'  => 465,
        'value2' => 744
    ),
    'section2' => array (
        'value'  => 6544,
        'value2' => 565
    ),
    'section5' => array (
        'value'  => 345,
        'value2' => 7465
    )
);

$sum = 0;
array_walk($array, function($a) use (&$sum){
    $sum2 += $a['value2'];
});

print_r($sum);
// 8774

Comments

0

I just used OOP to handle this

<?php
//Define class for calculating sum of value1 & value2
class _array {
    //Define variables
    private $sum_of_value = 0;
    private $sum_of_value_2 = 0;
    private $array = [];
    //Appending object in list
    public function append($title,$_value,$_value2)
    {
        $this->array[$title] = new obj($_value,$_value2);
        $this->sum_of_value = $this->sum_of_value + $_value;
        $this->sum_of_value_2 = $this->sum_of_value_2 + $_value2;
    }
    //Return list
    public function getArray()
    {
        var_dump($this->array);
    }
    //Return sum of value
    public function getSumOfValue()
    {
        echo $this->sum_of_value;
    }
    //Return sum of value2
    public function getSumOfValue2()
    {
        echo $this->sum_of_value_2;
    }

}
//Definiton of our object
class obj{
    public $value;
    public $value2;
    function __construct($value,$value2) {
        $this->value = $value;
        $this->value2 = $value2;
      }
}
//Enter values
$array = new _array();
$array->append('section1',465,744);
$array->append('section2',6544,565);
$array->append('section5',345,7465);
//Get array
$array->getArray();
//Get sum of value & sum of value 2
$array->getSumOfValue();
echo '<br>';
$array->getSumOfValue2();
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.