In php i have array like this
$the_array = Array(
[0]=>Array([id]=>1,[value]=>10,[name]=>apple),
[1]=>Array([id]=>1,[value]=>20,[name]=>orange),
[2]=>Array([id]=>1,[value]=>30,[name]=>banana),
[3]=>Array([id]=>2,[value]=>100,[name]=>car),
[4]=>Array([id]=>2,[value]=>200,[name]=>bicycle),
)
and try merge this arrays to gather
result i want is :
$result = Array(
[0]=>Array([id]=>1,[value]=>60,[name]=>"apple,orange,banana"), #value = 10+20+30
[1]=>Array([id]=>2,[value]=>300,[name]=>"car,bicyle"),
)
i try to do it with this way
function group_by($key, $data) {
$result = array();
foreach($data as $val) {
if(array_key_exists($key, $val)){
$result[$val[$key]][] = $val;
}else{
$result[""][] = $val;
}
}
return $result;
}
but it's not and result is wrong or incomplete
and i have no access to values for create array like result
Thank you for your help
question from:
https://stackoverflow.com/questions/65887917/merge-group-array-in-php 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…