这是我的php代码,我从mysql数据库中获取数据:
$result2 = $mysqli->query("SELECT * FROM movies");
$lista = array();
while ($row2 = $result2->fetch_array())
{
$ime=$row2['Title'];
$result1 = $mysqli->query("SELECT AVG(Rating) FROM ratings WHERE Title='$ime'");
while ($row1 = $result1->fetch_array()){
$lista = array
(
array($row2['Title'],round($row1['AVG(Rating)'],1),$row2['Year'])
);
}
}
var_dump($lista);
我得到了这个结果:
array (size=1)
0 =>
array (size=3)
0 => string 'Memento' (length=7)
1 => float 8.5
2 => string '2000' (length=4)
我认为问题在于每个新数组都会覆盖最后一个数组,而我只得到size = 1数组。那么,如何将数组添加到数组中而不是覆盖它呢?
我希望我的数组看起来像这样:
array (size=4)
0 =>
array (size=3)
0 => string 'Forrest Gump' (length=12)
1 => float 4.5
2 => string '1994' (length=4)
1 =>
array (size=3)
0 => string 'The Prestige' (length=12)
1 => float 7
2 => string '2006' (length=4)
2 =>
array (size=3)
0 => string 'The Usual Suspects' (length=18)
1 => float 7.5
2 => string '1995' (length=4)
3 =>
array (size=3)
0 => string 'Memento' (length=7)
1 => float 8.5
2 => string '2000' (length=4)
答案 0 :(得分:1)
你应该这样做:
$result2 = $mysqli->query("SELECT * FROM movies");
$lista = array();
while ($row2 = $result2->fetch_array())
{
$ime=$row2['Title'];
$result1 = $mysqli->query("SELECT AVG(Rating) FROM ratings WHERE Title='$ime'");
while ($row1 = $result1->fetch_array()){
$lista[] = array($row2['Title'],round($row1['AVG(Rating)'],1),$row2['Year']);
}
}
var_dump($lista);