topic description
how to add the same key values in a two-dimensional array
sources of topics and their own ideas
add up the sum of a hid with the same two-dimensional array
do it yourself
$array=[];
foreach ($arr as $k=>$v){
$array[$v["hid"]][]=$v;
}
$list=[];
foreach ($array as $k=>$v){
foreach ($v as $k1=>$v1){
$list[$k]+=$v1["sum"];
}
}
dump($list); array(4) {[2] => int(6) [3] => int(7)[5] => int(6) [4] => int(5)
related codes
/ / Please paste the code text below (please do not replace the code with pictures)
is to convert the following to $list= [6, 7, 6, 5];
$arr=[
["id"=>1,"hid"=>2,"sum"=>1],
["id"=>2,"hid"=>2,"sum"=>2],
["id"=>3,"hid"=>2,"sum"=>3],
["id"=>4,"hid"=>3,"sum"=>1],
["id"=>5,"hid"=>3,"sum"=>2],
["id"=>6,"hid"=>3,"sum"=>4],
["id"=>7,"hid"=>5,"sum"=>2],
["id"=>8,"hid"=>5,"sum"=>4],
["id"=>9,"hid"=>4,"sum"=>5]
];
}
what result do you expect? What is the error message actually seen?
how to optimize my code. I"ve written too many loops. What I want is the sum addition of the same hid