使用相同值对数组进行分组的最佳方法

时间:2014-09-10 14:12:07

标签: php arrays

我需要你的帮助来解决这个问题:)

我有这个数组

    Array ( 
    [0] => Array ( [order_id] => 121 [item_id] => 4344 [item_name] => Product [item_price] => 123 [item_type] => product [paypal_address] => email@test.com [qty] => 4 [currency] => EUR ) 
    [1] => Array ( [order_id] => 121 [item_id] => 3444 [item_name] => Product1 [item_price] => 444 [item_type] => product [paypal_address] => email@test.com [qty] => 2 [currency] => EUR ) 
    [2] => Array ( [order_id] => 121 [item_id] => 1233 [item_name] => Product2 [item_price] => 120 [item_type] => product [paypal_address] => email2@test.com [qty] => 18 [currency] => EUR ) 
)

我想循环它并按值将它们分组到一个新数组中。

例如:

选择数组中具有相同paypal_address总和价格的所有项目,总和数量并将其移动到新数组中。

这是我想要实现的,任何提示/建议?

修改 最后,我想要一个像这样或类似的数组:

Array ( 
    [0] => Array ( [order_id] => 121 [items_id] => array(4344, 3444) [items_name] => 'Product , Product1' [amt] => 567 [item_type] => product [paypal_address] => email@test.com [qty] => 8 [currency] => EUR )
    [1] => Array ( [order_id] => 121 [items_id] => 1233 [items_name] => Product2 [amt] => 120 [item_type] => product [paypal_address] => email2@test.com [qty] => 18 [currency] => EUR ) 
)

EDIT2: 到目前为止我做了什么。但它不能很好地阅读。

        $groupedParams = array();
        foreach($params as $key=>$param){
            if(!array_key_exists($param['paypal_address'], $groupedParams) && $param['item_type'] == 'product'){
                $groupedParams[$param['paypal_address']] = array(
                    'order_id' => $param['order_id'],
                    'item_id' => $param['item_id'],
                    'item_name' => $param['item_name'],
                    'qty' => $param['qty'],
                    'amt' => $param['item_price'],
                    'item_type' => $param['item_type']
                );
            }else if(array_key_exists($param['paypal_address'], $groupedParams) && $param['item_type'] == 'product'){
                $newItemId = $groupedParams[$param['paypal_address']]['item_id'].','.$param['item_id'];
                $newAmt = (int)$groupedParams[$param['paypal_address']]['amt']+(int)$param['item_price'];
                $newQty = (int)$groupedParams[$param['paypal_address']]['qty']+(int)$param['qty'];
                $newItemName = (string)$groupedParams[$param['paypal_address']]['item_name'].' - '.(int)$param['item_name'];

                $groupedParams[$param['paypal_address']] = array(
                    'order_id' => $param['order_id'],
                    'item_id' => $newItemId,
                    'item_name' => $newItemName,
                    'qty' => $newQty,
                    'amt' => $newAmt,
                    'item_type' => $param['item_type']
                );
            }
}

由于

3 个答案:

答案 0 :(得分:1)

您希望根据我的理解将其重组为更清晰的格式,只需输入:

$array = array();
foreach($params as $ar) {
    $array[$ar['paypal_address']][] = $ar;
}

示例:http://jdl-enterprises.co.uk/sof/25767688.php

答案 1 :(得分:0)

  1. 获取数组及其值
  2. 在paypal_address上创建唯一键
  3. 使用唯一键创建临时数组
  4. 使用临时数组中的相应唯一键存储所有值

        $arr =     Array (
            '0' => Array ( 'order_id' => 121 ,'item_id' => 4344 ,'item_name' => 'Product' ,'item_price' => 123 ,'item_type' => 'product' ,'paypal_address' => 'email@test.com' ,'qty' => 4 ,'currency' => 'EUR' ) ,
            '1' => Array ( 'order_id' => 121 ,'item_id' => 3444 ,'item_name' => 'Product1' ,'item_price' => 444 ,'item_type' => 'product' ,'paypal_address' => 'email@test.com' ,'qty' => 2 ,'currency' => 'EUR' ) ,
            '2' => Array ( 'order_id' => 121 ,'item_id' => 1233 ,'item_name' => 'Product2' ,'item_price' => 120 ,'item_type' => 'product' ,'paypal_address' => 'email2@test.com' ,'qty' => 18 ,'currency' => 'EUR' )
        );
        $tmpArr = Array();
        $cnt = sizeof($arr);
        for($i=0;$i<$cnt;$i++){
                $paypal_address = $arr[$i]['paypal_address'];
                if(!is_array($tmpArr[$paypal_address])){
                        $tmpArr[$paypal_address] = Array();
                }
                $tmpArr[$paypal_address]['paypal_address'] = $arr[$i]['paypal_address'];
                $tmpArr[$paypal_address]['order_id'] = $arr[$i]['order_id'];
                $tmpArr[$paypal_address]['item_name'][] = $arr[$i]['item_name'];
                $tmpArr[$paypal_address]['item_type'][] = $arr[$i]['item_type'];
                $tmpArr[$paypal_address]['currency'][] = $arr[$i]['currency'];
    
    
                $tmpArr[$paypal_address]['qty'] = isset($tmpArr[$paypal_address]['qty']) ? $tmpArr[$paypal_address]['qty'] + $arr[$i]['qty'] : $arr[$i]['qty'];
                $tmpArr[$paypal_address]['item_id'] = $arr[$i]['item_id'];
                $tmpArr[$paypal_address]['item_id'][] = $item_id;
                $tmpArr[$paypal_address]['item_price'] = isset($tmpArr[$paypal_address]['item_price']) ? $tmpArr[$paypal_address]['item_price'] + $arr[$i]['item_price'] : $arr[$i]['item_price'];
    
        }
        print_r($tmpArr);
    
  5. 阵列的输出如下:

        Array([email@test.com] => Array ([paypal_address] => email@test.com[order_id] => 121[item_name] => Array  ( [0] => Product [1] => Product1  )[item_type] => Array  ( [0] => product [1] => product  )[currency] => Array  ( [0] => EUR [1] => EUR  )[qty] => 6[item_id] => 3444[item_price] => 567 )[email2@test.com] => Array ([paypal_address] => email2@test.com[order_id] => 121[item_name] => Array  ( [0] => Product2  )[item_type] => Array  ( [0] => product  )[currency] => Array  ( [0] => EUR  )[qty] => 18[item_id] => 1233[item_price] => 120 ))
    

    注意:根据您的要求对代码进行更改

答案 2 :(得分:0)

    $details =Array ( 
        [0] => Array ( [order_id] => 121 [item_id] => 4344 [item_name] => Product [item_price] => 123 [item_type] => product [paypal_address] => email@test.com [qty] => 4 [currency] => EUR ) 
        [1] => Array ( [order_id] => 121 [item_id] => 3444 [item_name] => Product1 [item_price] => 444 [item_type] => product [paypal_address] => email@test.com [qty] => 2 [currency] => EUR ) 
        [2] => Array ( [order_id] => 121 [item_id] => 1233 [item_name] => Product2 [item_price] => 120 [item_type] => product [paypal_address] => email2@test.com [qty] => 18 [currency] => EUR ) 
    )
    $sorted = array();
    foreach ($details as $key => $value) {
      $add = $value['paypal_address'];
      foreach ($details as $index => $detail) {
        if ($add == $detail['paypal_address']) {
          foreach ($detail as $cat => $val) {
            if (!in_array($val, $sorted[$key][$cat])) {
              $sorted[$key][$cat][] = $val;
            }
          }
          unset($details[$index]);
        }
      }
    }
// Output array
print_r($sorted);