2

I have the following data that I parsed from an xml file and now I have a problem returning the data. How do i return the photo src data from the array within the photo array. Any ideas as to what I am doing wrong?

Code

$xml = simplexml_load_file($url);
$photo_url = $xml['photo']->src;

for ($i=0, $n=count($photo_url); $i<$n; ++$i) {
    echo $photo_url[$i].'<br>';
}

Data

["photo"]=>
  array(46) {
    [0]=>
    object(SimpleXMLElement)#2 (1) {
      ["@attributes"]=>
      array(6) {
        ["id"]=>
        string(5) "26001"
        ["src"]=>
        string(36) "1006416.jpg"
        ["thumb"]=>
        string(42) "1006416_thumb.jpg"
        ["title"]=>
        string(16) "album"
        ["subtitle"]=>
        string(6) "01.jpg"
        ["favorite"]=>
        string(0) ""
      }
    }
    [1]=>
    object(SimpleXMLElement)#3 (1) {
      ["@attributes"]=>
      array(6) {
        ["id"]=>
        string(5) "26001"
        ["src"]=>
        string(36) "1006417.jpg"
        ["thumb"]=>
        string(42) "1006417_thumb.jpg"
        ["title"]=>
        string(16) "album"
        ["subtitle"]=>
        string(6) "02.jpg"
        ["favorite"]=>
        string(0) ""
      }
    }

1 Answer 1

1

You should use the SimpleXMLElement attributes method, like so:

$xml = simplexml_load_file($url);
foreach( $xml as $xml_node)
{
    $attributes = $xml_node->attributes();
    echo 'Photo source: ' . $attributes['src'] . "\n";
}

Demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.