PHP 切换并将内容添加到一个变量

发布于 2024-11-27 00:13:20 字数 1180 浏览 2 评论 0原文

我正在使用这个函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

风情万种。 2024-12-04 00:13:20

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];
}

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];
}
泪之魂 2024-12-04 00:13:20

我能找到的最可扩展和最简单的(没有 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" />'

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" />'
意中人 2024-12-04 00:13:20

您写道 $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.

无所谓啦 2024-12-04 00:13:20

就这样做:

$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 中)

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)

_畞蕅 2024-12-04 00:13:20

首先,您需要初始化“$ret”变量,添加“$ret='';”在“foreach”循环之前。使用 if 和 elseif。

Firstly, you need to initialize your "$ret" variable, add "$ret='';" before the "foreach" loop. Use if and elseif.

澜川若宁 2024-12-04 00:13:20

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 = '毒、土、死亡';

$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;
    }
    /*
     * 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

?>

愿与i 2024-12-04 00:13:20
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;

}

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;

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文