PHP Switch and adding content to one variable
I'm using this function:
function makeImmunities($data) {
$immunities = explode(',', $data);
foreach($immunities as $immunity) {
switch($immunity) {
case 'poison':
$ret .= '<img src="/images/gems/earth.gif"/> ';
break;
case 'earth':
$ret .= '<img src="/images/gems/earth.gif"/> ';
break;
case 'paralyze':
$ret .= '<img src="/images/gems/paralyze.gif"/> ';
break;
case 'death':
$ret .= '<img src="/images/gems/death.gif"/> ';
break;
case 'ice':
$ret .= '<img src="/images/gems/ice.gif"/> ';
break;
case 'invisible':
$ret .= '<img src="/images/gems/invisible.gif"/> ';
break;
}
}
return $ret;
}
And $ret has .=, so content should be added to itself. But it doesn't. I wan't it to work this way:
$data has an array which looks like thi开发者_Python百科s : 'poison', 'earth', 'death'. And this function returns only first case:
$ret .= '<img src="/images/gems/earth.gif"/> ';
I wan't it too add ALL of the content that $ret has if case is the same as the $immunity.
Switch doesn't work like that you will need to use an if
and do something like this:
$options = array(
'poison' => '<img src="/images/gems/earth.gif"/> ',
'earth' => '<img src="/images/gems/earth.gif"/> '
'etc' => '...'
);
if(isset($options[$immunity])){
$ret .= $options[$immunity];
}
The most extensible and easiest I can find (without an if or even a loop)
function makeImmunities($data) {
$allImmunities = array(
'poison' => '/images/gems/earth.gif',
'earth' => '/images/gems/earth.gif',
'paralyze' => '/images/gems/paralyze.gif',
'death' => '/images/gems/death.gif',
'ice' => '/images/gems/ice.gif',
'invisible' => '/images/gems/invisible.gif',
);
$immunities = array_intersect_key($allImmunities, array_flip(explode(',', str_replace(' ','',$data))));
return implode(' ', array_map(function(&$item, $key) {
return "<img src=\"{$item}\" alt=\"{$key}\" />";
}, $immunities, array_keys($immunities)));
}
Exemple:
var_export(makeImmunities('ice,poison,death'));
Output
'<img src="/images/gems/earth.gif" alt="poison" /> <img src="/images/gems/death.gif" alt="death" /> <img src="/images/gems/ice.gif" alt="ice" />'
You wrote that $data
contains an array, but from the code it is clear, that it should contain the string, for example $data='earth,ice,poison';
. Notice, that there mustn't be any space. By the way, it is good to initialize (with empty string) the $ret
variable before foreach.
Just do it like this:
$immunities = explode(',', $data);
foreach($imminities as $key => $value) {
$ret .= "<img src='/images/gems/{$value}.gif" />";
}
return $ret;
You'll have to check for the immunities though. You could easily add a if clause to inside the foreach, something like
if($value == "earth" || $value == "water" ....)
Pretty simple way to do it.
Update As suggested by Cole, the part inside the if condition could be replaced with a in_array($value, $immunities_supported)
. I don't know about the efficiency of this though.
Remember you also have to add a $immunities_supported = array("earth", "water");
etc with the immunities that are present.
(The code below by Cole doesn't work directly though, it's just a concept, since $value is always in $immunities)
Firstly, you need to initialize your "$ret" variable, add "$ret='';" before the "foreach" loop. Use if and elseif.
function makeImmunities($data) {
$ret = ""; //initialise $ret
$immunities = explode(',', $data);
foreach($immunities as $immunity) {
switch($immunity) {
case 'poison':
$ret .= '<img src="/images/gems/earth.gif"/> ';
break;
case 'earth':
$ret .= '<img src="/images/gems/earth.gif"/> ';
break;
case 'paralyze':
$ret .= '<img src="/images/gems/paralyze.gif"/> ';
break;
case 'death':
$ret .= '<img src="/images/gems/death.gif"/> ';
break;
case 'ice':
$ret .= '<img src="/images/gems/ice.gif"/> ';
break;
case 'invisible':
$ret .= '<img src="/images/gems/invisible.gif"/> ';
break;
}
/*
* creates an array $match with key = immunity of match found e.g poison, death, earth
* and values equal to <img src="/images/gems/xxxxxx.gif"/> where xxxxxx is the
* value for each corresponding image tag.
*/
$match[$immunity] = $ret;
}
return $match;
}
$input = 'poison, earth, death';
$ans = makeImmunities($input); // returns an array
print_r($ans); //prints the array
?>
function makeImmunities($data) {
$ret = ""; //initialise $ret
$immunities = explode(',', $data);
foreach($immunities as $immunity) {
switch($immunity) {
case 'poison':
$ret .= '<img src="/images/gems/earth.gif"/> ';
break;
case 'earth':
$ret .= '<img src="/images/gems/earth.gif"/> ';
break;
case 'paralyze':
$ret .= '<img src="/images/gems/paralyze.gif"/> ';
break;
case 'death':
$ret .= '<img src="/images/gems/death.gif"/> ';
break;
case 'ice':
$ret .= '<img src="/images/gems/ice.gif"/> ';
break;
case 'invisible':
$ret .= '<img src="/images/gems/invisible.gif"/> ';
break;
}
$match[$immunity] = $ret;
}
return $match;
}
精彩评论