如何在php中基于键值合并数组?

时间:2013-08-14 06:19:06

标签: php arrays merge

我有一个看起来像这样的数组。我想按照他们的订单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 个答案:

答案 0 :(得分:1)

我认为这更为直截了当。

       $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);

答案 1 :(得分:0)

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

答案 2 :(得分:0)

使用php 5,启动快速类来对其进行建模,然后使用asort对它们进行排序,如果需要,可以将它们移动到另一个数组中。像这样:

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;
}
}

创建数组,使用“load_values_from_array”填写您的类,并按如下顺序排列:

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

如果您希望将键值作为orderID,则可以执行以下操作:

$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;
}

当然,你可以跳过课程部分并使用看起来和行为类似于最后一位的代码来解决它,如下所示:

$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;
}

我没有测试代码,但我认为你会抓住这个漂移。

答案 3 :(得分: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;
    }
}