[英]PHP Switch and adding content to one variable
我正在使用此功能:
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;
}
$ ret具有。=,因此应将内容添加到自身。 但事实并非如此。 我不想这样工作:
$ data具有一个看起来像这样的数组:'poison','earth','death'。 并且此函数仅返回第一种情况:
$ret .= '<img src="/images/gems/earth.gif"/> ';
如果大小写与$ immunity相同,我也不要添加$ ret具有的所有内容。
Switch不能那样工作,您将需要使用if
并执行以下操作:
$options = array(
'poison' => '<img src="/images/gems/earth.gif"/> ',
'earth' => '<img src="/images/gems/earth.gif"/> '
'etc' => '...'
);
if(isset($options[$immunity])){
$ret .= $options[$immunity];
}
您写道$data
包含一个数组,但是从代码中可以清楚地看出,它应该包含字符串,例如$data='earth,ice,poison';
。 请注意,不得有任何空格。 顺便说一句,最好在foreach之前初始化$ret
变量(使用空字符串)。
像这样做:
$immunities = explode(',', $data);
foreach($imminities as $key => $value) {
$ret .= "<img src='/images/gems/{$value}.gif" />";
}
return $ret;
不过,您必须检查免疫力。 您可以轻松地在foreach内部添加if子句,例如
if($value == "earth" || $value == "water" ....)
相当简单的方法。
更新如Cole所建议,if条件内的部分可以用in_array($value, $immunities_supported)
代替。 我不知道这样做的效率。 记住,您还必须添加$immunities_supported = array("earth", "water");
等具有的免疫力。 (Cole下面的代码虽然不能直接工作,但这只是一个概念,因为$ value 始终在$ immunities中)
我能找到的最可扩展和最容易的方法(没有if甚至没有循环)
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)));
}
范例:
var_export(makeImmunities('ice,poison,death'));
输出量
'<img src="/images/gems/earth.gif" alt="poison" /> <img src="/images/gems/death.gif" alt="death" /> <img src="/images/gems/ice.gif" alt="ice" />'
首先,您需要初始化“ $ ret”变量,添加“ $ ret =”;” 在“ foreach”循环之前。 使用if和elseif。
函数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 ='毒药,泥土,死亡';
$ ans = makeImmunities($ input); //返回一个数组
print_r($ ans); //打印数组
?>
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.