PHP SimpleXML finding a specific Attribute

I am using the Flickr API.  The XML below is loaded into an object called $flickrResponse using simplexml_load_string();

I need the source value of the size element where the label is "Thumbnail" (http://farm2.static.flickr.com/1103/567229075_2cf8456f01_t.jpg).  I tried the loop below...also tried a for each loop, but it's not working out.  Is there a way to do it without creating a loop?

Thanks in advance for any help with this!
Alex
<rsp stat="ok">
<sizes canblog="1" canprint="1" candownload="1">
<size label="Square" width="75" height="75"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_s.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/sq/"/>
<size label="Thumbnail" width="100" height="75"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_t.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/t/"/>
<size label="Small" width="240" height="180"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_m.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/s/"/>
<size label="Medium" width="500" height="375"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/m/"/>
<size label="Original" width="640" height="480"
      source="http://farm2.static.flickr.com/1103/567229075_6dc09dc6da_o.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/o/"/>
</sizes>
</rsp>
 
	while ($flickrResponse) {
	
		if ($flickrResponse->sizes->size[label] = "Thumbnail") {
		
			echo $flickrResponse->sizes->size[label];
		
			$photoThumb = $flickrResponse->sizes->size[source];
		}
	}

Open in new window

alexswebsiteAsked:
Who is Participating?
 
thehagmanCommented:
Within SimpleXML the solution should be something like


foreach ($flickrresponse->sizes->children() as $size) {
   if ($size[label] == 'Thumbnail') {
      $photoThumb = $size[source];
   }
}

Open in new window

0
 
thehagmanCommented:
If you also have the XML string, then a pattern match helps
$xmlstring = '<rsp stat="ok"><sizes ... </rsp>';
if (preg_match('/<size label="Thumbnail"[^>]+source="([^"]+)"/', $xmlstring, $matches) {
  echo  $matches[1];
} else {
  echo "No thumbnail";
}

Open in new window

0
 
Ray PaseurCommented:
Here is what I would do.  Object iterators using foreach() are very efficient.  HTH, ~Ray
<?php // RAY_xml_flickr.php
 
// TEST DATA
$xml = '<rsp stat="ok">
<sizes canblog="1" canprint="1" candownload="1">
<size label="Square" width="75" height="75"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_s.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/sq/"/>
<size label="Thumbnail" width="100" height="75"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_t.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/t/"/>
<size label="Small" width="240" height="180"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_m.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/s/"/>
<size label="Medium" width="500" height="375"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/m/"/>
<size label="Original" width="640" height="480"
      source="http://farm2.static.flickr.com/1103/567229075_6dc09dc6da_o.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/o/"/>
</sizes>
</rsp>';
 
// MAKE AN OBJECT FROM THE XML
$obj = SimpleXML_Load_String($xml);
 
// ITERATOR
foreach ($obj->sizes->size as $my_image)
{
 
// NOTE THE USE OF THE ASSOCIATIVE ARRAY INSIDE THE 'size' FIELD
   if ($my_image["label"] == "Thumbnail")
   {
      echo "<br/>{$my_image["source"]}\n";
   }
// END ITERATOR
}

Open in new window

0
 
alexswebsiteAuthor Commented:
This solution was perfect--concise and quick.  Thanks to everyone for the help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.