在php中添加关联数组值?

在php中添加关联数组值?,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我有一个具有以下属性的数组: Array ( [0] => Array ( [project] => test proposal [type] => pending [0] => 10,000 [1] => 10,000 [2] => 5,000 [3] => 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
        )

) 
我想将最后一项推送到数组中,该数组组合了所有其他项的值,project和type可以为空。因此,结果将是:

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;

您需要将
newArr
附加到
$array
。。是吗?@Stoic我们的答案看起来非常相似如果($v=='project')继续,你认为哪个更快或者你得到的
如果(是数值($key))
?我想我的代码会比你的代码快一点。005ms!!Bwa hawell,我希望我的代码保持简洁P他可以留一个对他来说更好看的。。但是,老实说。。为了得到真正正确的答案,我们需要添加
$sum=array('project'=>'','type'=>'')在最顶端;)我们都忘记了:P和父亲风暴保持着。
$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($array as $arr) {
    foreach($arr as $k => $v) {
        if($v== 'project' || $v == 'type') continue;
        $newArr[$k] = $newArr[$k] + $v;
    }
}
$array[] = $newArr;