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相結合,並從第二陣列創建嵌套數組。
請使用PHP var_export()當您發佈PHP數據構建這裏。這以PHP可執行代碼的形式爲您提供數據。我們可以複製/粘貼到演示原理的代碼示例中,而無需執行將print_r()數據轉換爲PHP代碼的漫長過程!謝謝。 –
我不認爲你會從股票PHP函數中得到這個。使用幾個'foreach'循環,它不應該超過幾行代碼。 – FKEinternet
你的問題不清楚,你真的想要什麼? –