如何在多级foreach中构建自定义数组?

时间:2015-03-10 00:15:01

标签: php arrays multidimensional-array foreach

这是原始数据

Array
(
    [name] => me
    [tickets] => Array
        (
            [1] => Array
                (
                    [equipment] => Array
                        (
                            [1] => Array
                                (
                                    [name] => DVR
                                    [received] => 10
                                )
                            [2] => Array
                                (
                                    [name] => DCT
                                    [received] => 3
                                )
                        )
                )
            [2] => Array
                (
                    [equipment] => Array
                        (
                            [1] => Array
                                (
                                    [name] => DVR
                                    [received] => 4
                                )
                            [2] => Array
                                (
                                    [name] => DCT
                                    [received] => 6
                                )
                        )
                )
        )
)

用户拥有多个故障单,但每个故障单都有相同的项目,具有不同的“已接收”金额。我想将sum收到的金额合并为一个变量/数组。

以下是我希望如何使其像

一样工作的演示
Array
(
    [name] => me
    [equipment] => Array
        (
            [DVR] => 14
            [DCT] => 9
        )
)

这是我最近尝试从多维数组构建自己的数组失败。

foreach($data as $user){
    $sum = [];
    $sum['name'] = $user->name;
    $sum['equipment'] = [];
    foreach($user->tickets as $ticket){
        foreach($ticket->equipments as $eqpt){
            $sum['equipment'][$eqpt['name']] += $eqpt['pivot']['received'];
        }
    }
    print_r($sum);
}

3 个答案:

答案 0 :(得分:0)

请尝试以下代码。但是,$data中只有一位用户,因此您需要先$data = [$data];

foreach ($data as $user) {
    $sum = [];
    $sum['name'] = $user['name'];
    $sum['equipment'] = [];
    foreach($user['tickets'] as $ticket){
        foreach($ticket['equipment'] as $eqpt){
            $sum['equipment'][$eqpt['name']] += $eqpt['received'];
        }
    }
    print_r($sum);
}

答案 1 :(得分:0)

试试这个:

$array = $data; //$data is your array

$sum = array('name' => $array['name'], 'equipment' => array());

foreach($array['tickets'] as $row) {
  for($i = 0; $i < count($row); $i++) {
      foreach($row['equipment'] as $infos) {
        $sum['equipment'][$infos['name']] += $infos['received'];
        //print_r($infos);
    }     
  }       
}   

print_r($sum);

答案 2 :(得分:0)

好吧,经过大量谷歌搜索和反复试验后,这似乎有效

$sum = [];
// $data is a collection returned by Laravel
// I am converting it to an array
foreach($data->toArray() as $user){
    $items = [];
    foreach($user['tickets'] as $ticket){
        foreach($ticket['equipments'] as $eqpt){
            $name = $eqpt['name'];

            if (! isset($items[$name]))
            {
                $items[$name] = $eqpt['received'];
            } else {
                $items[$name] += $eqpt['received'];
            }
        }
    }
    $sum[] = [
        'name' => $user['name'],
        'equipment' => $items
    ];
}

@tsnorri @Adrian Cid Almaguer