繁体   English   中英

在php中添加关联数组值?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM