问题描述
|
我有以下数组,里面满是乱码(我对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匹配的第一个数组上。
例如
因此,第一个数组将变为:
[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,但是它只对其中一个起作用。
解决方法
尝试这个:
foreach($array1 as $key=>&$arr){
$arr[\'replies\'] = $array2[$key][\'replies\']
}
,看来您也可以在桌子上进行JOIN操作
foreach($array1 as $key => $value)
{
$array1[$value[\'thread_id\']] = $value;
}
foreach($array2 as $value)
{
array_push($array1[$value[\'id\']],$value);
}
,如果我的问题正确,那么您想合并array2.id和array1.thread_id相同的数组。在这种情况下,您将无法使用array_merge。除非您将第二个数组更改为如下所示。
数组(
[$ thead_id] =>数组([回复] => 2)
....
)
那么您可以轻松地使用array_merge。