citricsquid Posted October 6, 2009 Share Posted October 6, 2009 <sizes> − <size name="original" width="300" height="300"> http://userserve-ak.last.fm/serve/_/2457865/Basshunter+126073.jpg </size> <size name="large" width="126" height="126">http://userserve-ak.last.fm/serve/126/2457865.jpg</size> <size name="largesquare" width="126" height="126">http://userserve-ak.last.fm/serve/126s/2457865.jpg</size> <size name="medium" width="64" height="64">http://userserve-ak.last.fm/serve/64/2457865.jpg</size> <size name="small" width="34" height="34">http://userserve-ak.last.fm/serve/34/2457865.jpg</size> <size name="extralarge" width="252" height="252">http://userserve-ak.last.fm/serve/252/2457865.jpg</size> </sizes> How would I get each size by their name? So if I wanted to call "small" or "extralarge"? $xmlData->images->image->sizes->size what comes after that? adding ['extralarge'] after size doesn't work. Quote Link to comment Share on other sites More sharing options...
salathe Posted October 6, 2009 Share Posted October 6, 2009 You could do something similar to the following code snippet. They key thing is the use of the SimpleXMLElement::xpath method. <?php // ... foreach ($xmlData->images->image as $image) { // SimpleXMLElement::xpath returns an array, we only want the first value $thumb = current($image->sizes->xpath('size[@name="small"]')); printf( '<img src="%s" width="%d" height="%d" alt="%s"><br>'."\n", $thumb, $thumb['width'], $thumb['height'], htmlspecialchars($image->title) ); } ?> The other option would be to manually loop over the different sizes available until you get to the small one, but the XPath way is much prettier. Quote Link to comment Share on other sites More sharing options...
citricsquid Posted October 6, 2009 Author Share Posted October 6, 2009 The other option would be to manually loop over the different sizes available until you get to the small one, but the XPath way is much prettier. I'll try that then, thanks I was considering looping, but I assumed it was simple enough to just add ['extralarge'] or an image size somewhere Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.