这个问题已经在这里有一个答案:
我用下面的代码来获取输出数据库:
$array = array();
while($row = ...){
$array[] = $row;
}
return $array;
现在,我可以得到的值:
foreach($array as $in){
echo $in[title];
}
但我想->
获取值:
foreach($array as $in){
echo $in->title;
}
您可以在while()
循环中存储对象。
使用库MySQLi,与fetch_object()
:
while ($row = $result->fetch_object()) {
// here, $row is an object
$array[] = $row;
}
使用PDO,以fetch()
和PDO::FETCH_OBJ
常数:
while ($row = $sth->fetch(PDO::FETCH_OBJ)) {
// here, $row is an object
$array[] = $row;
}
然后,你可以访问使用->
:
foreach($array as $in){
echo $in->title;
}
你可以尝试这样的
$object = new stdClass();
foreach ($array as $key => $value)
{
$object->$key = $value;
}
或者参考答案this