展示包含对象的数组的具体数据
展示包含对象的数组的具体数据
我想展示一个数组的特定数据,我有以下代码,并且我试图通过变量$name
来指示打印printConcreteStudent
函数中的特定学生。
当我尝试找到该学生时,我遇到以下错误:
致命错误:无法使用类型为Student的对象作为数组
数组的结构如下:
array(1) { [0]=> object(Student)#1 (4) { ["name"]=> string(5) "Student1" ["lastname":"Student":private]=> string(7) "lastName1" } }
我正在尝试使用以下函数打印特定数据:
function printConcreteStudent($name) { foreach($this->students as $key=>$value){ if($value["name"] == $name){ echo $value->getName() . " "; echo $value->getLastName() . " "; } } }