我正在使用PHP和MongoDB。我可以遍历数组并将每个值打印到屏幕上,但是当我只想打印1个值时,什么也没有出现,而且我也没有收到错误。
<?php
$mng = new MongoDB\Driver\Manager(
'mongodb+srv://removedUsername:removedPassword@cluster0-vqdka.mongodb.net/test?retryWrites=true');
$query = new MongoDB\Driver\Query([], [
'MenusAttached' => true,
]);
$rows = $mng->executeQuery('restaurantApp.restaurants', $query);
$emptyArray = [];
foreach ($rows as $row)
{
//echo $row->MenusAttached;
array_push($emptyArray, $row->MenusAttached);
}
echo "Starting";
//RELEVANT CODE BELOW.
for ($i = 0; $i < count($emptyArray); $i++) {
echo $emptyArray[$i]; //THIS WORKS
echo $emptyArray[1]; //BLANK, NOTHING COMES UP, NO ERROR. WHY?
}
?>
答案 0 :(得分:0)
1。而不是
array_push($emptyArray, $row->MenusAttached);
使用
$emptyArray[] = $row->MenusAttached;
2。像下面这样使用for()
代替foreach()
循环
foreach ( $emptyArray as $arr ){
print_r($arr); // check the value and then proceed accordingly
}