[英]Add associate array values in php?
我有一個具有以下屬性的數組:
Array
(
[0] => Array
(
[project] => test proposal
[type] => pending
[0] => 10,000
[1] => 10,000
[2] => 5,000
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
)
[1] => Array
(
[project] => test 3
[type] => won
[0] => 0
[1] => 0
[2] => 20,000
[3] => 20,000
[4] => 10,000
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
)
[2] => Array
(
[project] => Test 3
[type] => pending
[0] => 8,333
[1] => 8,333
[2] => 8,333
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
)
)
我想將最后一項推送到組合所有其他項的值的數組,項目和類型可以是空白的。 因此結果將是:
Array
(
[0] => Array
(
[project] => test proposal
[type] => pending
[0] => 10,000
[1] => 10,000
[2] => 5,000
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
)
[1] => Array
(
[project] => test 3
[type] => won
[0] => 0
[1] => 0
[2] => 20,000
[3] => 20,000
[4] => 10,000
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
)
[2] => Array
(
[project] => Test 3
[type] => pending
[0] => 8,333
[1] => 8,333
[2] => 8,333
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
)
[3] => Array
(
[project] =>
[type] =>
[0] => 18,333
[1] => 18,333
[2] => 33,333
[3] => 20,000
[4] => 10,000
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
[10] => 0
[11] => 0
)
)
或許這樣嗎?:
$temp=array('project'=>'','type'=>'');
foreach($array as $project=> $data){
foreach($data as $node=>$value){
if(is_int($node) && is_int($value)){
@$temp[$node]+=$value;
}
}
}
$array[]=$temp;
foreach ($data as $sub) {
foreach ($sub as $key => $value) {
if (is_numeric($key)) $sum[$key] += $value;
}
}
$data[] = $sum;
foreach($array as $arr) {
foreach($arr as $k => $v) {
if($v== 'project' || $v == 'type') continue;
$newArr[$k] = $newArr[$k] + $v;
}
}
$array[] = $newArr;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.