Php 按id和总和合并数组
我正在尝试仅通过choosen键合并和计算。例如,我有以下数组:Php 按id和总和合并数组,php,arrays,Php,Arrays,我正在尝试仅通过choosen键合并和计算。例如,我有以下数组: $array=array(); $array['Fruit']['id'=>17、'name'=>'季节性水果'、'type'=>'水果'、'measurement'=>''、'amount'=>2]; $array['Fruit']['id'=>17、'name'=>'季节性水果'、'type'=>'水果'、'measurement'=>''、'amount'=>1]; $array['protein']['id'=>16,'n
$array=array();
$array['Fruit']['id'=>17、'name'=>'季节性水果'、'type'=>'水果'、'measurement'=>''、'amount'=>2];
$array['Fruit']['id'=>17、'name'=>'季节性水果'、'type'=>'水果'、'measurement'=>''、'amount'=>1];
$array['protein']['id'=>16,'name'=>'大豆酸奶','type'=>'蛋白质','measurement'=>'','amount'=>2];
$array['protein']['id'=>18,'name'=>oilseed','type'=>protein','measurement'=>grip','amount'=>2];
$array['protein']['id'=>18,'name'=>oilseed','type'=>protein','measurement'=>grip','amount'=>1];
$array['lipid']['id'=>19,'name'=>椰子粉,'type'=>lipid','measurement'=>汤匙,'amount'=>2];
$array['lipid']['id'=>20,'name'=>巧克力粉,'type'=>lipid','measurement'=>汤匙,'amount'=>2];
$array['lipid']['id'=>38,'name'=>'chocolate square','type'=>'lipid','measurement'=>'','amount'=>1];
最终输出应如下所示:
Array (
[0] => Array (
[id] => 17
[name] => seasonal fruit
[type] => Fruit
[measurement] =>
[amount] => 3
)
[1] => Array (
[id] => 16
[name] => soy yogurt
[type] => protein
[measurement] =>
[amount] => 2
)
[2] => Array (
[id] => 18
[name] => oilseed
[type] => protein
[measurement] => grip
[amount] => 3
)
[3] => Array (
[id] => 19
[name] => coconut powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[4] => Array (
[id] => 20
[name] => chocolate powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[5] => Array (
[id] => 38
[name] => chocolate square
[type] => lipid
[measurement] =>
[amount] => 1
)
)
我尝试了以下方法:
$courses=[];
foreach($key=>$item的数组){
如果(!key_存在($key$courses)){
$courses[$key]=[];
}
foreach($row形式的项目){
$id=$row['id'];
如果(!key_存在($id,$courses[$key])){
$courses[$key][$id]=$row;
继续;
}
$r=&$courses[$key][$id];
$r['amount']+=$row['amount'];
}
}
输出:
Array (
[Fruit] => Array (
[17] => Array (
[id] => 17
[name] => seasonal fruit
[type] => Fruit
[measurement] =>
[amount] => 3
)
)
[protein] => Array (
[16] => Array (
[id] => 16
[name] => soy yogurt
[type] => protein
[measurement] =>
[amount] => 2
)
[18] => Array (
[id] => 18
[name] => oilseed
[type] => protein
[measurement] => grip
[amount] => 3
)
)
[lipid] => Array (
[19] => Array (
[id] => 19
[name] => coconut powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[20] => Array (
[id] => 20
[name] => chocolate powder
[type] => lipid
[measurement] => Tablespoon
[amount] => 2
)
[38] => Array (
[id] => 38
[name] => chocolate square
[type] => lipid
[measurement] =>
[amount] => 1
)
)
)
你离这里不远,但你感兴趣的一切都在里面,这样你就不会涉及到水果和蛋白质等
$courses = [];
foreach( $array as $sub){
foreach ($sub as $item) {
if ( array_key_exists($item['id'], $courses) ) {
// dup, so just add to the amount
$courses[$item['id']]['amount'] += $item['amount'];
} else {
//New entry
$courses[$item['id']] = $item;
}
}
}
// if its important to have indexes starting at 0 for the result
$courses = array_values($courses);
如果数组
$courses
与此相关,它看起来是什么like@RiggsFolly$courses
看起来像输出数组?哦,是的,我真傻]@riggsfully编辑过,也许它更可读