我需要在第二個級別上使用不同的鍵名稱合併兩個數組,但使用相同數量的鍵。用不同的鍵在第二級合併2個陣列
- 用戶A = 102
- 用戶userb = 103
我的期望的輸出將是陣列3
我已經嘗試了許多的變化得到這個在一起。
我最好的嘗試是像下面,但它破壞了我的鍵名:
$results = array();
foreach($arr1 as $key => $array)
{
foreach($array as $user => $value)
{
$results[$user]['name'] = $value;
}
}
foreach($arr2 as $key => $array)
{
foreach($array as $user => $value)
{
$results[$user]['name2'] = $value;
}
}
陣列1:
Array
(
[usera] => Array
(
[0] => Array
(
[user] => usera
[duration_s] => 15
)
[1] => Array
(
[user] => usera
[duration_s] => 9
)
)
[userb] => Array
(
[2] => Array
(
[user] => userb
[duration_s] => 21
)
[3] => Array
(
[user] => userb
[duration_s] => 19
)
)
)
陣列2:
Array
(
[102] => Array
(
[0] => Array
(
[user] => 102
[duration_s2] => 54
)
[1] => Array
(
[user] => 102
[duration_s2] => 378
)
)
[103] => Array
(
[2] => Array
(
[usernr] => 103
[duration_s2] => 299
)
[3] => Array
(
[usernr] => 103
[duration_s2] => 110
)
)
)
陣3:
Array
(
[usera] => Array
(
[0] => Array
(
[user] => usera
[duration_s] => 15
[usernr] => 102
[duration_s2] => 54
)
[1] => Array
(
[user] => usera
[duration_s] => 9
[usernr] => 102
[duration_s2] => 378
)
)
[userb] => Array
(
[2] => Array
(
[user] => userb
[duration_s] => 21
[usernr] => 103
[duration_s2] => 299
)
[3] => Array
(
[user] => userb
[duration_s] => 19
[usernr => 103
[duration_s2] => 110
)
)
)
也許你應該看看你如何創建原始2陣列。在那裏進行修改,以便創建所需的數組作爲該進程的一部分 – RiggsFolly
感謝您的回覆。陣列來自2個獨立的數據庫主機,所以我無法將它們連接在一起。 – Pandora
我們是否假設沒有邏輯意義的數組索引只是錯字?或者'Userb'的子數組真的包含索引'[2]&[3]'而不是'[0]&[1]' – RiggsFolly