在php中添加关联数组值?

时间:2011-01-04 22:00:49

标签: php arrays multidimensional-array

我有一个包含以下属性的数组:

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
        )
)

3 个答案:

答案 0 :(得分:2)

或许像这样?:

$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;

答案 1 :(得分:0)

foreach ($data as $sub) {
    foreach ($sub as $key => $value) {
        if (is_numeric($key)) $sum[$key] += $value;
    }
}
$data[] = $sum;

答案 2 :(得分:0)

foreach($array as $arr) {
    foreach($arr as $k => $v) {
        if($v== 'project' || $v == 'type') continue;
        $newArr[$k] = $newArr[$k] + $v;
    }
}
$array[] = $newArr;