PHP 切换并将内容添加到一个变量
我正在使用这个函数:
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 有一个如下所示的数组:“毒药”,“地球”,“死亡”。 这个函数只返回第一种情况:
$ret .= ' ';
如果大小写与 $immunity 相同,我不希望它也添加 $ret 具有的所有内容。
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 this : '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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Switch 的工作方式不同,您需要使用
if
并执行如下操作:Switch doesn't work like that you will need to use an
if
and do something like this:我能找到的最可扩展和最简单的(没有 if 甚至循环)
示例:
输出
The most extensible and easiest I can find (without an if or even a loop)
Exemple:
Output
您写道
$data
包含一个数组,但从代码中可以清楚地看出,它应该包含字符串,例如$data='earth,ice,poison';
。请注意,不能有任何空间。顺便说一下,最好在 foreach 之前初始化(使用空字符串)$ret
变量。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.就这样做:
不过,您必须检查免疫力。您可以轻松地在 foreach 内部添加一个 if 子句,就像
非常简单的方法一样。
更新 根据 Cole 的建议,if 条件内的部分可以替换为
in_array($value, $immunities_supported)
。但我不知道这样做的效率。请记住,您还必须添加
$immunities_supported = array("earth", "water");
等与现有的免疫。(Cole 下面的代码不能直接工作,它只是一个概念,因为 $value 总是在 $immunities 中)
Just do it like this:
You'll have to check for the immunities though. You could easily add a if clause to inside the foreach, something like
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)
首先,您需要初始化“$ret”变量,添加“$ret='';”在“foreach”循环之前。使用 if 和 elseif。
Firstly, you need to initialize your "$ret" variable, add "$ret='';" before the "foreach" loop. Use if and elseif.
function makeImmunities($data) {
}
$input = '毒、土、死亡';
$ans = makeImmunities($input); // 返回一个数组
print_r($ans); //打印数组
?>
function makeImmunities($data) {
}
$input = 'poison, earth, death';
$ans = makeImmunities($input); // returns an array
print_r($ans); //prints the array
?>
}
}