我試圖讓兩個數組數組(我認爲這意味着他們是多維的)通過共享值組合/合併在一起。換句話說,類似於SQL中的JOIN語句。有沒有一種特定的,有效的方法來解決這個問題?在PHP中組合這個數組數組的最有效方法是什麼?
試圖合併這兩個:
Array
(
[0] => Array
(
[0] => 5
[1] => alice
[2] => car
)
[1] => Array
(
[0] => 3
[1] => bob
[2] => truck
)
[2] => Array
(
[0] => 5
[1] => charlie
[2] => bike
)
)
Array
(
[0] => Array
(
[0] => 5
[1] => alice
[2] => Vancouver
)
[1] => Array
(
[0] => 3
[1] => bob
[2] => Toronto
)
[2] => Array
(
[0] => 5
[1] => charlie
[2] => Portland
)
)
生產出:
Array
(
[0] => Array
(
[0] => 5
[1] => alice
[2] => car
[3] => Vancouver
)
[1] => Array
(
[0] => 3
[1] => bob
[2] => truck
[3] => Toronto
)
[2] => Array
(
[0] => 5
[1] => charlie
[2] => bike
[3] => Portland
)
)
你想「加入ID」?可能最好將該id設爲數組索引。 – 2012-08-04 21:20:43
另外,我更喜歡第二級的arrray被關聯,不會混合 – 2012-08-04 21:21:50
與@eicto一致 - 如果你有一個帶有命名鍵的數組而不是[ 1],[2],[3]。事實上,我甚至可以說這是必需的,以使您的系統更加健壯。因爲誰說不可能有一個叫做'Bike'的城市,或者一個叫'Truck'的人?如果你有任何數據模糊的可能性,那麼你不能使用任何包含'array_unique'或'array_merge'等的答案。 – Spudley 2012-08-04 21:37:17