How php adds the values of the same key in a two-dimensional array

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

Apr.17,2022

<?php
$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]
];
$result = [];
foreach ($arr as $item) {
    // 
    isset($result[$item['hid']]) || $result[$item['hid']] = 0;
    $result[$item['hid']] += $item['sum'];
}
$result = array_values($result);

var_dump($result);
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e4c663-44eaf.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e4c663-44eaf.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?