如果我有一个数组$MovieDetails = array();
并且下面的查询使用foreach循环(5个元素; id, movie_year, genre, image, movie_name
)填充它,我该如何添加另一个元素(movie_rating
)到数组的结尾
$AllMovies = $con ->query
("
SELECT id, movie_year, genre, image, movie_name FROM movies;
");
while($row = $AllMovies->fetch_object()) {
$MovieDetails[] = $row;
}
答案 0 :(得分:3)
将电影评分添加到$row
。
如果您将其作为对象使用,则$row->movie_rating = 1.5
while($row = $AllMovies->fetch_object()) {
$row->movie_rating = 1.5;
$MovieDetails[] = $row;
}
如果您将其作为数组使用,请使用fetch_assoc()
和$row['movie_rating'] = 1.5
while($row = $AllMovies->fetch_assoc()) {
$row['movie_rating'] = 1.5;
$MovieDetails[] = $row;
}
答案 1 :(得分:1)
这样你的行就是一个对象
$AllMovies = $con->query("SELECT id, movie_year, genre, image, movie_name FROM movies;");
while($row = $AllMovies->fetch_object()) {
$row->movie_rating = 'movieRating';
$MovieDetails[] = $row;
}
如果你想让每一行都是数组,你应该这样做:
while($row = $AllMovies->fetch_array()) {
$row['movie_rating'] = 'movieRating';
$MovieDetails[] = $row;
}
答案 2 :(得分:0)
$MovieDetails['movie_rating'] = $movie_rating;