具有simplexml对象的数组。如何使用foreach循环获取它

时间:2011-11-06 03:29:55

标签: php curl foreach simplexml

我有这个代码用于解析xml url并将输出存储在$ query变量中。

 $url = 'http://www.slideshare.net/api/2/search_slideshows?api_key=GRCsS0fPo&
 ts=1320547556&hash=cf362f863d1f15e84c7a520804f7826731f958c4&q=electrical+engineering&
 page=4&download=0&items_per_page=25';

 echo $url;

 $ch=curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Your application name');

$query = curl_exec($ch);

curl_close($ch);
$array = (array) simplexml_load_string($query);
echo '<pre>';
print_r($array);

当我使用print_r($ array)打印这个数组时,它给了我

Array
   (
   [Meta] => SimpleXMLElement Object
    (
     [Query] => php programming
      [ResultOffset] => SimpleXMLElement Object
        (
        )

      [NumResults] => 25
      [TotalResults] => 36839
      )

      [Slideshow] => Array
     (
          [0] => SimpleXMLElement Object
        (
            [ID] => 1966058
            [Title] => title here
            [Description] => description here
            [Status] => 2
            [Username] =>usrname
            [URL] => url here
            [ThumbnailURL] => a url
            [ThumbnailSmallURL] => a url
               [Embed] => some embed code
    )
    [1] => SimpleXMLElement Object
        (
            [ID] => 1966058
            [Title] => title here
            [Description] => description here
            [Status] => 2
            [Username] =>usrname
            [URL] => url here
            [ThumbnailURL] => a url
            [ThumbnailSmallURL] => a url
            [Embed] => some embed code
    )

现在我想获取这个数组并使用它的每个值。我想要这个数组的所有节点值。所以如何使用foreach循环获取此数组。

1 个答案:

答案 0 :(得分:0)

我认为最好使用SimpleXML XPath方法而不是直接遍历数组。