Reputation: 5119
I have an array like this
$array = array('Autobus', 'Ecole', 'Hopital' ...);
And this is how I echo images that are in that array
foreach ($array as $prox){
if ($prox == 'Autobus'){
echo '<img src="./imgs/icones/autobus.png" width="35" height="35" alt="'.$prox.'" title="'.$prox.'"/>';
}
if ($prox == 'Ecole'){
echo '<img src="./imgs/icones/ecole.png" width="35" height="35" alt="'.$prox.'" title="'.$prox.'"/>';
}
if ($prox == 'Hopital'){
echo '<img src="./imgs/icones/hopital.png" width="35" height="35" alt="'.$prox.'" title="'.$prox.'"/>';
}
...
Is there any way shorter than that code to do the same thing ?
Thanks
Upvotes: 2
Views: 439
Reputation: 457
How about something like this?
The above posters did it how I was originally going to do it, but what if you have a word like "Chemin de fer"?
At least I think that's French...
$aArray = array
(
"autobus" => "Autobus",
"ecole" => "Ecole",
"hopital" => "Hopital",
);
foreach($aArray as $sImageName => $sTitle)
{
echo '<img src="/imgs/icones/'.$sImageName.'.png" width="35" height="35" title="'.$sTitle.'" />';
}
Tadaa?
Upvotes: 0
Reputation: 9884
foreach ($array as $prox){
sprintf('<img src="./imgs/icones/%s.png" width="35" height="35" alt="%s" title="%s"/>'
, strtolower($prox)
, $prox
, $prox);
}
This only works if the image name is the same (but lowercase) as $prox
Upvotes: 0
Reputation: 29025
foreach ($array as $prox){
$img = strtolower ($prox);
echo '<img src="./imgs/icones/'.$img.'autobus.png" width="35" height="35" alt="'.$prox.'" title="'.$prox.'"/>';
}
Upvotes: 0
Reputation: 40582
$array = array('Autobus', 'Ecole', 'Hopital');
foreach($array as $prox) {
echo '<img src="./imgs/icones/'.strtolower($prox).'.png" width="35" height="35" alt="'.$prox.'" title="'.$prox.'"/>';
}
Upvotes: 0
Reputation: 78006
foreach ($array as $prox){
echo '<img src="./imgs/icones/' . strtolower($prox) . '.png" width="35" height="35" alt="'.$prox.'" title="'.$prox.'"/>';
}
Upvotes: 5