0
 $xml=simplexml_load_string($lists) or die("Error: Cannot create object");
print_r($xml);

gives:

SimpleXMLElement Object ( 
    [list] => Array ( 
        [0] => SimpleXMLElement Object ( 
            [@attributes] => Array ( 
                [id] => 8 
                [name] => #1 
                [subscriber_count] => 210 
                [display_name] => Display name 
            ) 
        ) 
        [1] => SimpleXMLElement Object ( 
            [@attributes] => Array ( 
                [id] => 9 
                [name] => #2 No External Promotions 
                [subscriber_count] => 2242 
                [display_name] => Display name 
            ) 
        ) 
        [2] => SimpleXMLElement Object ( 
            [@attributes] => Array ( 
                [id] => 939036 
                [name] => #1 No Internal Promotions 
                [subscriber_count] => 3301 
                [display_name] => Display name 
        ) 
    )
)

how do I use a foreach loop to extract 'id' and other info.

vanadium23
  • 3,516
  • 15
  • 27
LTech
  • 1,677
  • 5
  • 29
  • 48
  • possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – michi Apr 30 '15 at 13:15
  • Recommended section in the PHP manual: [Basic SimpleXML usage](https://php.net/manual/en/simplexml.examples-basic.php) – hakre Apr 30 '15 at 14:44

1 Answers1

1

To access data you need to know about xml structure. As I see from dump you can access all first list node's attributes like this:

$xml=simplexml_load_string($lists) or die("Error: Cannot create object");
foreach($xml->list[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}

For all nodes example:

foreach($xml->list as $list){
    echo (string) $list, "\n";
    foreach($list->attributes() as $a => $b) {
       echo $a,'="',$b,"\"\n";
    }
}
vanadium23
  • 3,516
  • 15
  • 27
  • How can I specify to get just say the [id]? I need to then run a query to insert the info in the database so I need each attribute individually. – LTech May 03 '15 at 07:16