0

I use the following to take an array called $list and turn it into URLs:

function genIMG($sValue) {
return 'http://asite.com/'.$sValue.'?&fmt=jpg';
}
$IMGurls = array_map("genIMG", array_unique($list));
foreach($IMGurls as $imgLink) {
echo "<a href='". $imgLink ."'>". $imgLink ."</a><br />";
}

This works, but I also have some null values in the array. How can I have the array map ignore any values of null? Otherwise it just creates something like this: http://asite.com/?&fmt=jpg with no file name since it was null.

thindery
  • 1,164
  • 4
  • 23
  • 40

1 Answers1

6

You $list must have contained empty values use array_filter

    $IMGurls = array_map("genIMG", array_unique(array_filter($list)));

Example

$list = array(1,2,3,4,5,"","",7);

function genIMG($sValue) {
    return 'http://asite.com/' . $sValue . '?&fmt=jpg';
}

$IMGurls = array_map("genIMG", array_unique(array_filter($list)));
foreach ( $IMGurls as $imgLink ) {
    echo "<a href='" . $imgLink . "'>" . $imgLink . "</a><br />";
}

Output

http://asite.com/1?&fmt=jpg
http://asite.com/2?&fmt=jpg
http://asite.com/3?&fmt=jpg
http://asite.com/4?&fmt=jpg
http://asite.com/5?&fmt=jpg
http://asite.com/7?&fmt=jpg
Baba
  • 94,024
  • 28
  • 166
  • 217