简体   繁体   中英

Add associate array values in php?

I have an array with the following properties:

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
        )

) 

I'd like to push a last item to the array that combines the values of all of the others, project and type can be blank. So the outcome would be:

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

like this perhaps?:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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