我使用以下内容来调用一个数组$list并将其转换为URL:
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 />";
}
Run Code Online (Sandbox Code Playgroud)
这有效,但我null在数组中也有一些值.如何让数组映射忽略null的任何值?否则它只是创建这样的东西:
http://asite.com/?&fmt=jpg没有文件名,因为它是null.
您$list必须使用包含空值array_filter
$IMGurls = array_map("genIMG", array_unique(array_filter($list)));
Run Code Online (Sandbox Code Playgroud)
例
$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 />";
}
Run Code Online (Sandbox Code Playgroud)
产量
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
Run Code Online (Sandbox Code Playgroud)