1
<?php
$json = "http://pastebin.com/raw.php?i=e1Sw66C3";
$data = json_decode(file_get_contents($json), true);

$data = $data['recenttracks'];
$tracks=$data['track'];

 foreach ($tracks as $track) {
    $artist = $track['artist']['#text'];
    $title = $track['name'];
    $url = $track['url'];
    $image = array_reduce($track['image'], function ($image, array $i) { return $image ?: ($i['size'] == 'large' ? $i['#text'] : null); });
echo '<li><a rel="external nofollow" href="'.htmlentities($url, ENT_QUOTES, "UTF-8").'" title="', $title, '">', $artist, ' - ', $title, '</a></li>'; }
echo ($image);
?>

This snippet has always worked. Now I don't know why BOOM echo ($image); outputs nothing. I can't figure out what's wrong with that function. The rest of the code works fine (the other info taken from the input). You can check the input by going to the link in file_get_contents.

MultiformeIngegno
  • 6,959
  • 15
  • 60
  • 119

1 Answers1

0

As I wrote in comments, previously your code was working only because element with size = 'large' was the last one, otherwise variable $image is overwritten at every loop. What you need is something like this

$json = "http://pastebin.com/raw.php?i=e1Sw66C3";
$data = json_decode(file_get_contents($json), true);

$data = $data['recenttracks'];
$tracks=$data['track'];
$images = array();

 foreach ($tracks as $track) {
    $artist = $track['artist']['#text'];
    $title = $track['name'];
    $url = $track['url'];
    if (isset($track['image']) && is_array($track['image']))
       foreach($track['image'] as $image)
          if (isset($image['size']) && $image['size'] == 'large' &&
              isset($image['#text']) && !empty($image['#text']))
             $images[] = $image['#text'];

    echo '<li><a rel="external nofollow" href="' . 
          htmlentities($url, ENT_QUOTES, "UTF-8") . '" title="', $title, '">',
          $artist, ' - ', $title, '</a></li>'; 
}
echo join("\n", $images);
Cheery
  • 16,063
  • 42
  • 57
  • I'll try in some hours! Thanks :) – MultiformeIngegno Nov 06 '14 at 06:54
  • Thanks!!! It works! Just one last thing! How can I extract just the last ("large") image from the array? – MultiformeIngegno Nov 06 '14 at 19:13
  • `end($images)`, for example + http://stackoverflow.com/questions/3687358/best-way-to-get-last-element-of-an-array-w-o-deleting-it – Cheery Nov 06 '14 at 19:27
  • You mean `echo end($images);` instead of `echo join("\n", $images);` at the end? `echo end(array_values($images));` outputs nothing – MultiformeIngegno Nov 06 '14 at 19:30
  • Did I write anything about `array_values`?? `echo end($images);` – Cheery Nov 06 '14 at 19:33
  • Because the last element is empty, let me check why. Because in the original data there are empty values. They can be skipped by additional condition - added it to the updated answer. – Cheery Nov 06 '14 at 19:35
  • And yes and no. First of all, it rewrites $images at every loop, second - it will work only if the last element of the loop has requires size = large in it. Otherwise it is rewritten by null. Even if the last element is not empty, but does not have size = large, you will get nothing. – Cheery Nov 06 '14 at 22:21