简体   繁体   中英

reform of multidimentional array in php

I have an array like :

[emp_name1] => Array
        (
            [projectArray] => Array
                (
                    [2015-06-02] => Array
                        (
                            [estimated_time] => 6.00
                            [cost] => 570.00
                        )

                    [2015-06-03] => Array
                        (
                            [estimated_time] => 8.00
                            [cost] => 760.00
                        )

                )

        )

The output must be :

data[emp_name1][projectArray][estimated_time]=14
data[emp_name1][projectArray][cost]=1330.00

I did not understand how to redesign the above array to get the expected array.

I have tried as follows:

<?php foreach($row as $empName => $empArrayValue){
foreach(array_unique($projectArray) as $k => $pname){
   $total[$pname]["estimated_time"] += $row[$empName][$pname]["estimated_time"];
   $total[$pname]["cost"] += $row[$empName][$pname][$m]["cost"];
}
}

Please help me. Thanks in advance.

I guess whats your looking for is something like this

 $new_arr = array();
      foreach($arr as $emp => $emp_arr){
            foreach($arr[$emp] as $projectArray => $projectArray_arr){
                foreach($arr[$emp][$projectArray] as $date => $value3){
                    @$new_arr[$emp][$projectArray]['estimated_time'] += $arr[$emp][$projectArray][$date]['estimated_time'];
                    @$new_arr[$emp][$projectArray]['cost'] += $arr[$emp][$projectArray][$date]['cost'];
                }

            }
      }

I think you are missing a foreach loop:

<?php foreach($row as $empName => $empArrayValue){
    foreach($empArrayValue as $pname => $projectArray){
        foreach($projectArray as $k => $dateArray){
            $total[$empName][$pname]["estimated_time"] += $dateArray["estimated_time"];
            $total[$empName][$pname]["cost"] += $dateArray["cost"];
        }
    }
}?>

Solution using only FOR LOOP .

$k=array_keys($emp_name1['projectArray']);
$sum1=0;
$sum2=0;
for($v=0;$v<count($emp_name1['projectArray']);$v++)
{

        $sum1=$sum1+$emp_name1['projectArray'][$k[$v]]["estimated_time"]+0;
        $sum2=$sum2+$emp_name1['projectArray'][$k[$v]]["cost"]+0;
}

echo $sum1;
echo "<br/>";
echo $sum2;

I won't recommend silencing your code at any circumstances so best not to use @ and instead check if the key been set previously or not.

here is a functioning prototype to demonstrate how it can be achieved.

<?php
$your_main_array = [
    'emp_name1' => [
        'projectArray' => [
            '2015-06-02' => [
                'estimated_time' => 6.00,
                'cost' => 570.00,
            ],
            '2015-06-03' => [
                'estimated_time' => 8.00,
                'cost' => 7600.00,
            ]
        ]
    ]
];

$empName1 = 'emp_name1';
$sumTimeEstimateKey = 'estimated_time';
$sumCostKey = 'cost';
$result = [$empName1 =>[]];

foreach ($your_main_array[$empName1] as $projectName => $projects) {
    if (!isset($result[$empName1][$projectName])) {
        $result[$empName1][$projectName] = [
            $sumTimeEstimateKey => 0.0,
            $sumCostKey => 0.0,
        ];
    }

    foreach ($projects as $project) {
        $result[$empName1][$projectName][$sumTimeEstimateKey] += $project[$sumTimeEstimateKey];
        $result[$empName1][$projectName][$sumCostKey] += $project[$sumCostKey];

    }
}

var_dump($result);

output:

array(1) {
    'emp_name1' =>
    array(1) {
        'projectArray' =>
        array(2) {
            'estimated_time' =>
            double(14)
        'cost' =>
        double(8170)
        }
    }
}

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