使用SimpleXML在PHP中迭代XML数据

I have the following Simple XML Data:

  [Listeners] => 37
            [listener] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [IP] => 0.0.0.1
                            [UserAgent] => curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
                            [Connected] => 1236
                            [ID] => 120
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [IP] => 0.0.0.2
                            [UserAgent] => curl/7.19.7 (i386-redhat-linux-gnu) libcurl/7.19.7 NSS/3.16.2.3 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
                            [Connected] => 1235
                            [ID] => 121
                        )

I want to show [IP].

So I have this PHP script:

<?php

$url = "http://admin:password@media.xseu.net/admin/listclients?mount=/FavoriteFM";
$xml = simplexml_load_file($url);
echo "<pre>";
print_r($xml);
 $IP = $xml->listener->IP;
 echo $IP[0];

?>

But it's not working. How can I display display IP in the proper way?

Just change this line:

(You simply forgot the first level source)

$IP = $xml->listener->IP;

to this:

$IP = $xml->source->listener->IP;
        //^^^^^^^^ See here

This should work for you:

foreach($xml->source->listener as $ip) 
    echo $ip->IP . "<br />"; 

$url = "http://admin:password@media.xseu.net/admin/listclients?mount=/FavoriteFM";
$xml = simplexml_load_file($url);
$IP = $xml->source->listener;
    foreach($IP as $row){
      echo $row->IP;
    }

You will get all the IPs of array.