1

我有一个看起来像这样的数组。我想按他们的订单 ID 合并数组。

大批(

[0] => Array
    (
        [orderId] => 152
        [prodName] => Red Dri-fit Undershirt
        [quantity] => 2
        [cartId] => 677
    )

[1] => Array
    (
        [orderId] => 151
        [prodName] => Practice Shorts
        [quantity] => 2
        [cartId] => 667
    )

[2] => Array
    (
        [orderId] => 151
        [prodName] => Red Dri-fit Undershirt
        [quantity] => 2
        [cartId] => 668
    )

)

它应该看起来像这样。

大批 (

[152] => Array
    (
        [prodName] => Red Dri-fit Undershirt
        [quantity] => 2
        [cartId] => 677
    )

[151] => Array
    (

    [1] => Array
            (
                    [prodName] => Practice Shorts
                    [quantity] => 2
                    [cartId] => 667
            )

        [2] => Array
            (
                [prodName] => Red Dri-fit Undershirt
                    [quantity] => 2
                    [cartId] => 668
            )

)

)

我正在通过它们的订单 ID 尝试两个这两个数组。我正在寻找一个函数(如果有的话)将值保存或合并到类似的键中,但到目前为止还没有运气。

4

4 回答 4

1

I think this is much more straight forward.

       $collections = array();    
        foreach($products as $product){
            if(!empty($collections[$product['orderId']]) || !isset($collections[$product['orderId']])){
                array_push($collections[$product['orderId']],
                    array(
                        'prodName' => $product['prodName'],
                        'quantity' => $product['quantity'],
                        'cartId' => $product['cartId'],
                        'isPack' => $product['isPack']
                    )            
                );

            }else{
                $collections[$product['orderId']] = array(
                    array(
                        'prodName' => $product['prodName'],
                        'quantity' => $product['quantity'],
                        'cartId' => $product['cartId'],
                        'isPack' => $product['isPack']
                    )                                          
                );        
            }
        }
echo '<pre>';
print_r($collections);
于 2013-08-14T06:31:18.677 回答
0

With php 5, whip up a quick class to model it, then use asort to sort them and, if you want, move them into another array. Like this:

class MyThing
{
private orderID;
private prodName;
private quantity;
private cartID;
private isPack;

public get_OrderID() {return $this->orderID;}

public load_values_from_array(array &$data)
{
$this->orderID=$data['orderId'];
$this->prodName=$data['prodName'];
/* ... go on ...*/
}

public static order(MyThing &$a, MyThing &$b)
{
if($a->orderID > $b->orderID) return 1;
else if($a->orderID < $b->orderID) return -1;
else return 0;
}
}

Create the array, fill it up with your classes using "load_values_from_array" and order it like this:

uasort($my_array, array('MyThing', 'order'));

If you want to have the key values as the orderID you can do this:

$order_key_id=array();
foreach($my_array as $key => &$value)
{
 $orderid=$value->get_orderID();
 if(!isset($order_key_id[$orderid])) $order_key_id[$orderid]=array();
 $order_key_id[$orderid][]=$value;
}

Of course, you can skip the class part and go nuts on it using code that looks and behaves like the last bit, like this:

$order_key_id=array();
foreach($my_array as $key => &$value)
{
     $orderid=$value['orderID'];
     if(!isset($order_key_id[$orderid])) $order_key_id[$orderid]=array();
     unset($value['orderID'];
     $order_key_id[$orderid][]=$value;
}

I did not test the code, but I think you'll catch the drift.

于 2013-08-14T06:32:30.867 回答
0
     $order_key_id=array();
     foreach($my_array as $key => &$value)
     {
      $orderid=$value->get_orderID();
      $order_key_id[$key][]=$value;
     }
于 2013-08-14T06:27:15.410 回答
0
$orders = array(
    array (
      'orderId' => 152,
      'prodName' => 'Red Dri-fit Undershirt'
    ),
    array (
      'orderId' => 151,
      'prodName' => 'Red Dri-fit Undershirt'
    ),
    array (
      'orderId' => 151,
      'prodName' => 'Red Dri-fit Undershirt'
    )
);
$orders_byid = array();
foreach($orders as $v){
    $order = $orders_byid[$v['orderId'];
    if ($order){
         if(!is_array($order[0])){
             unset($orders_byid[$v['orderId']);
             $orders_byid[$v['orderId'][] = $order;
         }
         $orders_byid[$v['orderId'][] = $v;
    } else {
        $orders_byid[$v['orderId'] = $v;
    }
}
于 2013-08-14T06:38:51.897 回答