我使用oop从数据库获取数据,它以数组形式提供数据
array(23) {
[0]=> array(6) {
[0]=> string(1) "6"
["id"]=> string(1) "6"
[1]=> string(17) "Office Management"
["Name"]=> string(17) "Office Management"
[2]=> string(1) "3"
["top_parent_id"]=> string(1) "3"
}
}
当我尝试从数组中获取数据时,它会给出错误
"注意:尝试在第12行和第34行的C:\ xampp \ htdocs \ oop \ test.php中获取非对象的属性;
这是我的代码
require_once('class.php');
$myClassObj = new db();
//$myClassObj->db();
$data = $myClassObj->select("parent_menu");
//var_dump($data);
foreach($data as $list)
{
echo $list->{'0'}->id."<br>";
}
答案 0 :(得分:1)
$data
是array
,您尝试访问object
之类的值。试试 -
echo $list[0]['id']."<br>";
答案 1 :(得分:0)
我看到了结果。 $data
对象包含所有数据,$list
是数组。您可以使用索引或键来从列表中获取数据,如
$list['id'];
$list[0];
两者都可以使用
答案 2 :(得分:0)
$list->{'0'}->id."<br>";
表示您尝试访问对象
但你有数组数组所以试试: -
echo $list[0]['id'];
答案 3 :(得分:0)
array(23) {
[0]=> array(6) {
[0]=> string(1) "6"
["id"]=> string(1) "6"
[1]=> string(17) "Office Management"
["Name"]=> string(17) "Office Management"
[2]=> string(1) "3"
["top_parent_id"]=> string(1) "3"
}
[1] => array(6) {
[0]=> string(1) "7"
["id"]=> string(1) "7"
[1]=> string(17) "Office Management"
["Name"]=> string(17) "Office Management"
[2]=> string(1) "3"
["top_parent_id"]=> string(1) "3"
}
}
如果以上数据是
的样本结果$data = $myClassObj->select("parent_menu");
然后你有一个多维数组
因为你正在使用foreach
foreach($data as $list)
{
// $list means you are accessing the index by index of
// $data like $data[0], $data[1] until to the end of the
// array
// so list is an array already with properties
// to access it
$list['id'];
$list['Name'];
$list['top_parent_id'];
// to add some error checking you can use
// isset to check if property is existing to the array
if (isset($list['id'])) {
}
}