Cakephp结合了两个或多个数组

时间:2017-08-09 21:00:45

标签: php arrays

我如何组合这个数组?

(int) 0 => array(
    'Test' => array(
        'id' => '108',
        'bericht' => '12',
        'fzge' => '35'
        'treiber' => 'Analyse'
    )
),
(int) 1 => array(
    'Test' => array(
        'id' => '109',
        'bericht_id' => '12',
        'fzge' => '25',
        'treiber' => 'Analyse'
    )
)

我想要的结果是:' Analyze' => ' 60',所以我想总结一下" fzge"如果" treiber"是一样的。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

使用以下代码。考虑到('测试',' treiber'' fzge')数组的键对于所有数组保持相同

// declare new array
$new_array = array();
foreach($array as $key=>$value){
    $new_array[$value['Test']['treiber']] = (isset($new_array[$value['Test']['treiber']])) ? $new_array[$value['Test']['treiber']]+$value['Test']['fzge'] : $value['Test']['fzge'];
}
print_r($new_array);

输出:

Array
(
    [Analyse] => 60
)