在PHP中对数组进行分组

时间:2017-12-13 10:24:31

标签: php arrays

我在PHP中有一个像这样的编号数组

原始阵列:

Array
        (
            [i] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [qty] => 5
                        )

                    [1] => Array
                        (
                            [id] => 2
                            [qty] => 5
                        )

                    [2] => Array
                        (
                            [id] => 2
                            [qty] => 5
                        )

                    [3] => Array
                        (
                            [id] => 1
                            [qty] => 5
                        )
                    [4] => Array
                        (
                            [id] => 3
                            [qty] => 5
                        )    
                )

        )

我希望它将相同的" id"如果有重复项,请加上数量,如果" id"是键而不是数字键,我应该能够做到。

我的预期结果是:

Array
        (
            [i] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [qty] => 10
                        )

                    [1] => Array
                        (
                            [id] => 2
                            [qty] => 10
                        )

                    [2] => Array
                        (
                            [id] => 3
                            [qty] => 5
                        )    
                )

        )

4 个答案:

答案 0 :(得分:1)

工作解决方案

<?php 
$your_arr = array(
    array('id' => 1,'qty' => 5),
    array('id' => 2,'qty' => 5),
    array('id' => 2222,'qty' => 5),
    array('id' => 1,'qty' => 5),
    array('id' => 3,'qty' => 5)
);
$new = array();
foreach ($your_array as $r){
    if(!isset($new[$r['id']]))$t=0; //check the current id exist in $new if Not $t = 0; 
    else $t=$r['qty']; //if yes $t's value become the saved value in $new[$r['id']]
    $new[$r['id']]['id'] = $r['id'];
    $new[$r['id']]['qty'] = ($t+$r['qty']); // add the new value with $new[$r['id]]'s value.

}
echo "<pre>";print_r($new);
?>

答案 1 :(得分:0)

这可以帮助您将id作为关键

$filtered_array = array();
foreach($arrayvariable as $eachdata)
{
  $filtered_array[$eachdata['id']][] =  $eachdata;
}
var_dump($filtered_array);exit;

谢谢,

答案 2 :(得分:0)

使用array_column可能有更优雅的方法,但我会执行以下操作:

  1. 按ID
  2. 对数组排序
  3. 循环结果并汇总部分
  4. 排序:

    usort($array, function($a, $b) { return $a['id'] <=> $b['id']; }); /* PHP 7 */
    

答案 3 :(得分:-1)

除了编写自己的方法处理创建目标数组的原始数组之外,我认为还有其他方法。像这样:

private function processArray($original) {
    $target = array();

    foreach ($original as $originalEntry) {
        foreach ($target as $targetEntry) {
            if ($targetEntry['id'] === $originalEntry['id']) {
                $targetEntry['qty'] = $targetEntry['qty'] + $originalEntry['qty'];
                continue 2;
            }
        }
        $target[] = $originalEntry;
    }
    return $target;
}

我没有时间测试此方法,但它应该使用相同的id连接所有条目,将所有qty字段相加。