php - Best way to group array with same value -
i need solve :)
i've array
    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 )  )   i loop on , group them values new array.
for example:
pick items in array have same paypal_address sum price, sum qty , move new array.
this want achieve, tip / suggestion ?
edit: @ end want array or similar:
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: did far. doesn't work , not read.
        $groupedparams = array();         foreach($params $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']                 );             } }   thanks
you want reorganise cleaner format understand, put:
$array = array(); foreach($params $ar) {     $array[$ar['paypal_address']][] = $ar; }        
Comments
Post a Comment