2016-11-10 109 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這樣的選擇列是一個更好的解決方案。