2016-11-10 51 views
-2

我在数组中遇到了一些问题。 我有3个数组。我想让1行称为历史,然后根据shipment_id插入该行。如果相同shipment_id,它将分组相同的货件ID。见下面的预期结果。如何将数组存储在数组中php

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。它有任何方法吗?

+0

无法帮助您输出!应该发布你的尝试代码... –

+0

@ jitendrapurohit..im粘贴我试过的代码。 – ZackZeidy

+0

@ DavidJorHpan.Pasted它.. – ZackZeidy

回答

1

您可以使用简单的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; 
     } 
    } 
    } 
} 

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

+1

为什么我没有想到'array_column',我似乎总是忘记这个函数的存在。 – Xorifelse

0

至于所需的输出:

# `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's这样的选择列是一个更好的解决方案。