Jump to content

Recommended Posts

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.