我从xml文件中解析了以下数据,现在我在返回数据时遇到了问题。我如何从照片数组内的数组返回照片src数据。你知道我做错了什么吗?
$xml = simplexml_load_file($url);
$photo_url = $xml['photo']->src;
for ($i=0, $n=count($photo_url); $i<$n; ++$i) {
echo $photo_url[$i].'<br>';
}
["photo"]=>
array(46) {
[0]=>
object(SimpleXMLElement)#2 (1) {
["@attributes"]=>
array(6) {
["id"]=>
string(5) "26001"
["src"]=>
string(36) "1006416.jpg"
["thumb"]=>
string(42) "1006416_thumb.jpg"
["title"]=>
string(16) "album"
["subtitle"]=>
string(6) "01.jpg"
["favorite"]=>
string(0) ""
}
}
[1]=>
object(SimpleXMLElement)#3 (1) {
["@attributes"]=>
array(6) {
["id"]=>
string(5) "26001"
["src"]=>
string(36) "1006417.jpg"
["thumb"]=>
string(42) "1006417_thumb.jpg"
["title"]=>
string(16) "album"
["subtitle"]=>
string(6) "02.jpg"
["favorite"]=>
string(0) ""
}
}
应该使用SimpleXMLElement属性方法,如下所示:
$xml = simplexml_load_file($url);
foreach( $xml as $xml_node)
{
$attributes = $xml_node->attributes();
echo 'Photo source: ' . $attributes['src'] . "n";
}
演示