问题描述
存储在 $ g
中的结果是1和2.下面编写的以下代码,即我的 $ array ['music']
仅存储最后一个元素那是2.但是我想要在foreach下执行2次sql查询,并将 $ g
的值与 mu_id
(mu_id
是另一个表 music
中的列名,并将第1行和第2行的所有行数据存储到 $ array ['music']
中.
The result stored in $g
is 1 and 2. The following code I've written below, my $array['music']
stores only the last element that is 2. But what I want is to execute my sql query 2 times under foreach and match the value of $g
which is 1 and 2 with mu_id
(mu_id
is the column name from another table music
) and store all the rows data of row 1 and 2 in to $array['music']
.
它仅存储第二行(2)而不存储1,或者在第二次执行内部循环时覆盖它.如果有任何逻辑可以使其正常工作,请告诉我.
It is storing only for the second row (2) not for 1 or it is overwriting it when it is executing for the second time inside loop. If there is any logic to make it work then please let me know.
foreach($genre as $g)
{
echo $g;
echo "<br>";
$array['music'] = $m -> where('mu_id', $g ) -> get();
}
推荐答案
您每次都在重新声明整个数组,而不是添加整个数组,而是使用以下声明:
You're redeclaring the entire array each time rather than adding to it, use this instead:
foreach($genre as $g)
{
$array['music'][] = $m->where('mu_id', $g)->get();
}
甚至更好,更少的查询:
Or even better, less queries:
$array['music'] = $m->where_in('mu_id', $genre)->get();
这篇关于数组被php中的最后一个元素覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!