繁体   English   中英

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

[英]How to merge array based on their key by value in php?

我有一个看起来像这样的数组。我想按他们的订单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尝试这两个数组中的两个。 我正在寻找一个函数来保存值或将值合并到类似的键中,但到目前为止还算运气。

我认为这更直接。

       $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);
     $order_key_id=array();
     foreach($my_array as $key => &$value)
     {
      $orderid=$value->get_orderID();
      $order_key_id[$key][]=$value;
     }

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

我没有测试代码,但我认为您会发现麻烦。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM