Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何对多维数组中具有相同属性的值求和?_Php_Arrays - Fatal编程技术网

Php 如何对多维数组中具有相同属性的值求和?

Php 如何对多维数组中具有相同属性的值求和?,php,arrays,Php,Arrays,我有一个包含乘积的数组,我想添加相同大小的乘积之和。例如,在我的I阵列中,我有5 Gal和30 Gal大小。我想要5加仑和30加仑的总和(在我的数组中,5加仑=10,30加仑=9)。我太累了,想不出给我这个输出的方法…请帮助。谢谢 array(7) { [0]=> object(stdClass)#223 (9) { ["size"]=> string(6) "30 GAL" ["list_price"]=> string(3) "614

我有一个包含乘积的数组,我想添加相同大小的乘积之和。例如,在我的I阵列中,我有5 Gal和30 Gal大小。我想要5加仑和30加仑的总和(在我的数组中,5加仑=10,30加仑=9)。我太累了,想不出给我这个输出的方法…请帮助。谢谢

array(7) {
  [0]=>
  object(stdClass)#223 (9) {
    ["size"]=>
    string(6) "30 GAL"
    ["list_price"]=>
    string(3) "614"
    ["count"]=>
    string(1) "2"
  }
  [1]=>
  object(stdClass)#224 (9) {
    ["size"]=>
    string(5) "5 Gal"
    ["list_price"]=>
    string(3) "131"
    ["count"]=>
    string(1) "3"
  }
  [2]=>
  object(stdClass)#225 (9) {
    ["size"]=>
    string(6) "30 GAL"
    ["list_price"]=>
    string(3) "727"
    ["count"]=>
    string(1) "4"
  }
  [3]=>
  object(stdClass)#226 (9) {
    ["size"]=>
    string(5) "5 Gal"
    ["list_price"]=>
    string(3) "138"
    ["count"]=>
    string(1) "1"
  }
  [4]=>
  object(stdClass)#227 (9) {
    ["size"]=>
    string(6) "30 GAL"
    ["list_price"]=>
    string(3) "804"
    ["count"]=>
    string(1) "3"
  }
  [5]=>
  object(stdClass)#228 (9) {
    ["size"]=>
    string(5) "5 Gal"
    ["list_price"]=>
    string(3) "176"
    ["count"]=>
    string(1) "4"
  }
  [6]=>
  object(stdClass)#229 (9) {
    ["size"]=>
    string(5) "5 Gal"
    ["list_price"]=>
    string(3) "182"
    ["count"]=>
    string(1) "2"
  }
}

这很容易。按如下方式使用foreach循环和总和计数值:

$count5GAL = 0;
$count30GAL = 0;

foreach($array as $value) {
    if ($value["size"] == "5 Gal") $count5GAL += $value["count"];
    else if ($value["size"] == "30 Gal") $count30GAL += $value["count"];
}
如果您有更多的GAL尺寸,您可以尝试动态执行:

$counter = array();

foreach($array as $value) {
    $keyName = str_replace(' ','',$value["size"]);
    if (!array_key_exists($counter,$keyName) array[$keyName] = $value["count"];
    else array[$keyName] += $value["count"];
} 

假设
$collection
是对象的数组

$sum5gal = 0;
$sum30gal = 0;
foreach ($collection as $obj){
    switch($obj->size){
        case "5 gal":
            $sum5gal++;
        break;
        case "30 gal":
            $sum30gal++;
        break;
    }
}

print "5 gal: " . $sum5gal . PHP_EOL;
print "30 gal: " . $sum30gal . PHP_EOL;


提示:使用
array\u sum()
每个对象要求和的属性是什么?价格还是计数?或者每个对象中有多少个匹配
大小
?我希望我的输出是另一个具有5个Gall=??而30Gal=?问题是我没有只有5 Gal或30 Gal。我不能有更多或更少的Gal尺寸。。。
$sum = array(
    '5gal'  => $sum5gal,
    '30gal' => $sum30gal
)
print_r($sum);