0
我有幾個關聯數組,我想將它們組合成一個關聯數組。例如:PHP將另一個關聯數組中的元素附加到具有相同鍵的相同數組中
$array_1 = Array
(
[0] => Array ([user_id] => 111 [first_name] => Jack [last_name] => Scotts [email])
[1] => Array ([user_id] => 222 [first_name] => David [last_name] => Weathers [email])
[2] => Array ([user_id] => 333 [first_name] => Helen [last_name] => Reynolds [email])
)
$array_2 = Array
(
[111] => Array
(
[0] => Array([user_account] => 111_001 [account_type] => normal)
[1] => Array([user_account] => 111_002 [account_type] => vip)
)
[222] => Array
(
[0]=> Array([user_account] => 222_01 [account_type] => normal)
)
[333] => Array
(
[0]=> Array([user_account] => 333_01[account_type] => vip)
)
)
Results:
$new_array = Array
(
[0] => Array
(
[user_id] => 111
[first_name] => Jack
[last_name] => Scotts
[account_data] = > Array
(
[0] => Array([user_account] => 111_001 [account_type] => normal)
[1] => Array([user_account] => 111_002 [account_type] => vip)
)
)
[1] => Array
(
[user_id] => 222
[first_name] => David
[last_name] => Weathers
[account_data] => Array
(
[0]=> Array([user_account] => 222_01 [account_type] => normal)
)
)
[2] => Array
(
[user_id] => 333
[first_name] => Helen
[last_name] => Reynolds
[account_data] => Array
(
[0] => Array ([user_account] => 333_01 [account_type] => vip)
)
)
)
嘗試array_merge和array_merge都遞歸,但它不起作用。
作爲一般的音符,有很多的問題在這裏一起「如何轉換<一些奇怪的數據線結構>變成<這個不錯的人>「。儘管在大多數情況下答案很簡單,但我總是問自己,這個奇怪的結構是從哪裏來的,是不是更容易改進_那部分。 – georg 2014-11-25 09:44:04