fanfavorite Posted January 22, 2009 Share Posted January 22, 2009 I have a variable ($img) with an array inside a SimpleXMLElement Object. Here is the printed variable: SimpleXMLElement Object ( [@attributes] => Array ( [src] => images/cclogos.gif [alt] => [width] => 151 [height] => 50 ) ) I can get each value by using $img['src'], $img['width'], etc, however I want to use a foreach command to get all the keys (src,width, etc) and values (images/cclogo.gif,151,50). I tried the following options with no success: foreach ($img as $key=>$im) { foreach ($img->{@attributes} as $key=>$im) { I can't figure it out. Any help is appreciated. Thanks! Link to comment https://forums.phpfreaks.com/topic/142041-solved-foreach-simplexmlelement-object/ Share on other sites More sharing options...
premiso Posted January 22, 2009 Share Posted January 22, 2009 I am not 100% familiar with the simple xml, and more code might help but I think this would work: <?php $string = <<<XML <a xmlns:b> <foo name="one" game="lonely">1</foo> </a> XML; $xml = simplexml_load_string($string); foreach($xml->foo[0]->attributes() as $a => $b) { echo $a,'="',$b,"\"\n"; } ?> The above is an example taken from SimpleXML->atrribute Hopefully that helps ya. Link to comment https://forums.phpfreaks.com/topic/142041-solved-foreach-simplexmlelement-object/#findComment-743791 Share on other sites More sharing options...
fanfavorite Posted January 23, 2009 Author Share Posted January 23, 2009 Thanks that lead me to the answer. Apparently it's $img->attributes(). The @attributes was throughing me off in the string. Link to comment https://forums.phpfreaks.com/topic/142041-solved-foreach-simplexmlelement-object/#findComment-743897 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.