我有以下数组,它充满了乱语(我厌倦了使用lorem ipsum所以我开始输入随机的东西 - 我正在测试¬_¬)。
我用mysqli_fetch_array来获取这个数组。
[ARRAY1]
Array
(
[0] => Array
(
[title] => this is a new thread, and it is great and it should work
[thread_id] => 27
[content] => <p>hello, how are you? and what are you doing y'all and this should work</p>
[username] => umar
[author_id] => 12
[tags] => Array
(
[0] => lorem
)
)
[1] => Array
(
[title] => this is my second thread and it should work fine, just fine
[thread_id] => 28
[content] => <p>this is is good, that I think it should have a thread of its own, don't you think?</p>
[username] => umarrazzaq
[author_id] => 12
[tags] => Array
(
[0] => thread
[1] => less
)
)
)
我有另一个数组[array2]:
Array ( [0] => Array ( [replies] => 2 [id] => 27 )
[1] => Array ( [replies] => 1 [id] => 28 ) )
我想将第二个数组推送到ID匹配的第一个数组。
e.g。
所以第一个数组将成为:
[0] => Array
(
[title] => this is a new thread, and it is great and it should work
[thread_id] => 27
[content] => <p>hello, how are you? and what are you doing y'all and this should work</p>
[username] => umar
[author_id] => 12
[tags] => Array
(
[0] => lorem
)
**[replies] => 2**
)
我尝试通过引用传递并在foreach循环中使用array_push,但它只能用于一个。
答案 0 :(得分:0)
试试这个:
foreach($array1 as $key=>&$arr){
$arr['replies'] = $array2[$key]['replies']
}
答案 1 :(得分:0)
您似乎也可以通过桌面上的JOIN进行此操作
foreach($array1 as $key => $value)
{
$array1[$value['thread_id']] = $value;
}
foreach($array2 as $value)
{
array_push($array1[$value['id']], $value);
}
答案 2 :(得分:0)
如果我的问题正确,那么你想合并array2.id和array1.thread_id相同的数组。在这种情况下,您不能使用array_merge。除非您将第二个数组更改为如下所示。
阵列( [$ thead_id] =&gt;数组([回复] =&gt; 2) .... ) 那么你可以轻松使用array_merge。