【问题标题】:How to merge array based on their key by value in php?如何在php中根据它们的键值合并数组?
【发布时间】:2013-08-14 06:19:06
【问题描述】:

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

【问题讨论】:

    标签: php arrays merge


    【解决方案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);
    

    【讨论】:

    • 非常感谢@Solano...:)
    【解决方案2】:

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

      【讨论】:

        【解决方案4】:
             $order_key_id=array();
             foreach($my_array as $key => &$value)
             {
              $orderid=$value->get_orderID();
              $order_key_id[$key][]=$value;
             }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-02-22
          • 2016-11-17
          • 2016-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多