2016-11-10 15 views
-2

配列に問題があります。 私は3つの配列を持っています。私は履歴と呼ばれる1行を作成したいと思って、その行をshipment_idに基づいて挿入します。同じ出荷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を比較する方法はわかりません。それはどんな方法がありますか?

+0

あなたの出力を助けることはできません!あなたの試したコードを投稿する必要があります... –

+0

@ jitendrapurohit..imは、私が試みたコードを貼り付けます。 – ZackZeidy

+0

@ DavidJorHpan..pasted it .. – 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

:NO履歴が見つからない場合、この方法は、shipment_idの値を保持する

# `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; 
     } 
    } 
} 

しかし、jitendrapurohit'sのようなセパレートカラムを作成する方がはるかに良い解決策です。

関連する問題