如何存储数组在数组php


how to store array in array php

我在数组中有一些问题。我有3个数组。我想创建一个名为history的行,然后根据shipment_id插入该行。如果相同的shipment_id,它将分组相同的发货id。

Array a ( 
 [0] => Array ( 
    [order_id] => 231 
    [name] => "One Layer Lunch Box 
    [quantity] => 1 
    [username] => linkath 
    [shipment_id] => SI000116 ) 
 [1] => Array ( 
    [order_id] => 231 
    [name] => "Test 03 
    [quantity] => 1 
    [username] => happymart 
    [shipment_id] => SI000117 ) )
Array b ( 
 [0] => Array ( 
    [shipment_id] => SI000116
    [date] => 2016-11-09 09:40:26 
    [status] => Ready to ship 
    [description] => Your item(s) is being packed and ready for shipment at our vendor's warehouse. ) 
 [1] => Array ( 
    [shipment_id] => SI000116
    [date] => 2016-11-09 10:16:04 
    [status] => Shipped 
    [description] => Your item(s) is being shipped with . For more details on your shipment, please go to Tracking, enter your tracking number ) )
Array c ( 
 [0] => Array ( 
    [shipment_id] => SI000117
    [date] => 2016-11-09 15:27:45 
    [status] => Ready to ship 
    [description] => Your item(s) is being packed and ready for shipment at our vendor's warehouse. ) )

符合预期数组

Array a ( 
[0] => Array ( 
    [order_id] => 231 
    [name] => "One Layer Lunch Box 
    [quantity] => 1 
    [username] => linkath 
    [shipment_id] => SI000116
    [history] => Array b ( 
                [0] => Array ( 
                    [shipment_id] => SI000116
                    [date] => 2016-11-09 09:40:26 
                    [status] => Ready to ship 
                    [description] => Your item(s) is being packed and ready for shipment at our vendor's warehouse. ) 
                [1] => Array ( 
                    [shipment_id] => SI000116
                    [date] => 2016-11-09 10:16:04 
                    [status] => Shipped 
                    [description] => Your item(s) is being shipped with . For more details on your shipment, please go to Tracking, enter your tracking number ) ) ) 
[1] => Array ( 
    [order_id] => 231 
    [name] => "Test 03 
    [quantity] => 1 
    [username] => happymart 
    [shipment_id] => SI000117 
    [history] => Array c ( 
                [0] => Array ( 
                    [shipment_id] => SI000117
                    [date] => 2016-11-09 15:27:45 
                    [status] => Ready to ship 
                    [description] => Your item(s) is being packed and ready for shipment at our vendor's warehouse. ) )
            ) 
    )
希望你明白我的意思。提前谢谢。

我试过的这段代码。

//tracker
        $getshipment = $this->model_account_order->getshipmentByOrder($this->request->get['order_id']);
        foreach ($getshipment as $shipment) {
            $data['shipment'][] = array(
                'shipment_id'   => $shipment['shipment_id']
            );
        }
        $i=0;
        foreach ($data['shipment'] as $key) {
            $gethistorybysid[$i] = $this->model_account_order->getHistory($data['shipment'][$i]['shipment_id']);
            $i++;
        }
        $u=0;
        foreach ($getshipment as $final) {
            $data['final'][] = array(
                'name'          => $shipment['name'],
                'quantity'      => $shipment['quantity'],
                'shipment_id'   => $shipment['shipment_id'],
                'history'       => array($gethistorybysid[$u])
            );
            $u++;
        }
        print_r($data['final']);

这个代码我不知道如何比较相同的货物id。有办法吗?

您可以使用简单的foreach()array_column来获得您想要的结果。你可以试试下面的方法,一定有效。

//Loop the main Array A as follows
foreach ($arrayA as $val) {
  //Take the array B and C in a loop and check for shipment Id
  foreach (array($arrayB, $arrayC) as $history) {
    // Push the array in history key(of Array A) if the match is found.
    if (in_array($val['shipment_id'], array_column($history, 'shipment_id')) {
      foreach ($history as $value) {
        $val['history'][] = $value;
      }
    }
  }
}

这只是一个想法,我认为这应该可以工作,如果不行,你可以根据你的需要修改

对于期望的输出:

# `array` is your first array where you want the shipments to be merged in.
# `array_b` is the array containing the history.
foreach($array as $contents){
    if(isset($contents['shipment_id'])){
        unset($match);
        foreach($array_b as $key => $value){
            if(isset($value['shipment_id'])){
                if($value['shipment_id'] == $contents['shipment_id']){
                    $match[] = $value['shipment_id'];
                }
            }
        }
        if(!empty($match)){
          $contents['shipment_id'] = $match;
        }
    }
}

当没有找到历史记录时,此方法也将保留shipment_id的值。

但是,像jitendrapurohit的答案那样创建一个单独的列是更好的解决方案。