如何遍历SimpleXMLElement对象?这就是我所拥有的:我试图以多种方式进行迭代,但没有成功:
Array
(
[status] => 1
[data_nilai_siswa] => SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[id_siswa] => 1
[nama_siswa] => 1
[n_bahasa] => 1.00
[n_matematika] => 1.00
[n_binggris] => 1.00
)
[1] => SimpleXMLElement Object
(
[id_siswa] => 2
[nama_siswa] => 2
[n_bahasa] => 2.00
[n_matematika] => 2.00
[n_binggris] => 2.00
)
)
)
)
我使用库休息客户端philsturgeon/codeigniter-restclient和脚本休息客户端:
$xmlResponse = $this->rest->get('http://127.0.0.1/EJ/testclient2/getdata/nilai');
$projects = array();
foreach($xmlResponse->project as $project) {
$projects[$project->id_siswa] = $project->nama_siswa;
echo 'Project ' . $project->nama_siswa . ' has ID ' . $project->id_siswa . PHP_EOL;
}
我收到错误消息:试图获取非对象的属性?如何解决?
答案 0 :(得分:1)
将其直接指向item
属性。
foreach($xmlResponse->data_nilai_siswa->item as $project) {
$projects[$project->id_siswa] = $project->nama_siswa;
echo 'Project ' . $project->nama_siswa . ' has ID ' . $project->id_siswa . '<br/>';
}