2017-07-17 40 views
0

我有一個數組,看起來像 $ firstArray通過特定的字段合併兩個數組

[0] => Array(
    [ID] => 1 
    [Fruit] => Apple 
    [State] => Ohio 
    ) 
[1] => Array(
    [ID] => 2 
    [Fruit] => Orange 
    [State] => Hawaii 
    ) 
[2] => Array(
    [ID] => 3 
    [Fruit] => Orange 
    [State] => Vermont 
    ) 

而另一個陣列,它看起來像 $ secondArray

[0] => Array(
    [ID] => 1 
    [description] => This is sample description 
    [Price] => 20 
    ) 
[1] => Array(
    [ID] => 1 
    [Fruit] => This is sample description 2 
    [Price] => 15 
    ) 
[2] => Array(
    [ID] => 2 
    [Fruit] => This is the second description 
    [Price] => 100 
    ) 
[3] => Array(
    [ID] => 2 
    [Fruit] => This is the second description 2 
    [Price] => 50 
    ) 
[4] => Array(
    [ID] => 3 
    [Fruit] => This is the third description 
    [Price] => 99 
    ) 

我用

$newArray = array_merge_recursive($firstArray, $secondArray); 

這隻附加了第二個數組中的條目到t的末尾他第一陣列理想我想新的陣列看起來像這樣

[0] => Array(
    [ID] => 1 
    [Fruit] => Apple 
    [State] => Ohio 
    [description] 
     Array(
     [0] => This is sample description 
     [1] => This is sample description 2 
    ) 
    [price] 
     Array(
     [0] => 20 
     [1] => 15 
    ) 
[1] => Array(
    [ID] => 1 
    [Fruit] => Apple 
    [State] => Ohio 
    [description] 
     Array(
     [0] => This is the second description 
     [1] => This is the second description 2 
    ) 
    [price] 
     Array(
     [0] => 100 
     [1] => 50 
    ) 

本質上新的陣列應在ID相結合,並從第二陣列創建嵌套數組。

+2

請使用PHP var_export()當您發佈PHP數據構建這裏。這以PHP可執行代碼的形式爲您提供數據。我們可以複製/粘貼到演示原理的代碼示例中,而無需執行將print_r()數據轉換爲PHP代碼的漫長過程!謝謝。 –

+2

我不認爲你會從股票PHP函數中得到這個。使用幾個'foreach'循環,它不應該超過幾行代碼。 – FKEinternet

+0

你的問題不清楚,你真的想要什麼? –

回答

1

看看這是否合理。 https://iconoun.com/demo/temp_jumpman.php

<?php // demo/temp_jumpman.php 
 
/** 
 
* Dealing with PHP nested arrays 
 
* 
 
* https://stackoverflow.com/questions/45145282/merge-two-arrays-by-specific-fields 
 
*/ 
 
error_reporting(E_ALL); 
 
echo '<pre>'; 
 

 
// TEST DATA CREATED FROM THE POST AT STACK - ID, Fruit, State 
 
$first = Array(
 
'0' => [ 
 
    'ID' => '1', 
 
    'Fruit' => 'Apple', 
 
    'State' => 'Ohio', 
 
    ], 
 
'1' => [ 
 
    'ID' => '2', 
 
    'Fruit' => 'Orange', 
 
    'State' => 'Hawaii', 
 
    ], 
 
'2' => [ 
 
    'ID' => '3', 
 
    'Fruit' => 'Orange', 
 
    'State' => 'Vermont', 
 
    ], 
 
); 
 

 
// TEST DATA CREATED FROM THE POST AT STACK - ID, description, Price 
 
$second = Array(
 
'0' => [ 
 
    'ID' => '1', 
 
    'description' => 'This is sample description', 
 
    'Price' => '20', 
 
    ], 
 
'1' => [ 
 
    'ID' => '1', 
 
    'description' => 'This is sample description 2', 
 
    'Price' => '15', 
 
    ], 
 
'2' => [ 
 
    'ID' => '2', 
 
    'description' => 'This is the second description', 
 
    'Price' => '100', 
 
    ], 
 
'3' => [ 
 
    'ID' => '2', 
 
    'description' => 'This is the second description 2', 
 
    'Price' => '50', 
 
    ], 
 
'4' => [ 
 
    'ID' => '3', 
 
    'description' => 'This is the third description', 
 
    'Price' => '99', 
 
    ], 
 
); 
 

 
$out = []; 
 
foreach ($first as $arr) 
 
{ 
 
    // PLACEHOLDERS FOR THE FOUND DATA FROM THE SECOND ARRAY 
 
    $arr['description'] = []; 
 
    $arr['Price'] = []; 
 
    $id = $arr['ID']; // THE ID WE WANT IN THE SECOND ARRAY 
 
    foreach ($second as $idp) 
 
    { 
 
     if ($idp['ID'] == $arr['ID']) 
 
     { 
 
      $arr['description'][] = $idp['description']; 
 
      $arr['Price'][] = $idp['Price']; 
 
     } 
 
    } 
 
    $out[] = $arr; 
 
} 
 
print_r($out);