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.

0

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.