【问题标题】:PHP Switch and adding content to one variablePHP切换并将内容添加到一个变量
【发布时间】:2011-07-27 00:04:29
【问题描述】:

我正在使用这个功能:

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 .= '&lt;img src="/images/gems/earth.gif"/&gt; ';

如果大小写与 $immunity 相同,我也不想添加 $ret 所具有的所有内容。

【问题讨论】:

    标签: php variables switch-statement


    【解决方案1】:

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

    【讨论】:

    • 当然开关就是这样工作的。你没看懂这个问题吧? ;)
    • @jacek switch 可能会这样工作,但无论如何这是使用 switch 的更好解决方案。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      您写道$data 包含一个数组,但从代码中可以清楚地看出,它应该包含字符串,例如$data='earth,ice,poison';。注意,不能有任何空间。顺便说一句,最好在 foreach 之前初始化(使用空字符串)$ret 变量。

      【讨论】:

        【解决方案4】:

        就这样吧:

        $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 下面的代码并不能直接工作,它只是一个概念,因为在 $immunities 中 $value always

        【讨论】:

        • 对于后者,您可以使用if(in_array($value,$immunities))
        • 哦,对了。不过,我尽量避免 in_array 和这些循环的东西,因为如果 $immunities 变大,一个 in_array 可能会使用大量资源。虽然那个巨大的如果也可以使用它......呃。弱论点,我知道。
        【解决方案5】:

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

        【讨论】:

        • 您不需要初始化$ret。你应该,但你不必 - foreach 没有自己的范围,所以变量是在第一次迭代中创建的并且它是持久的。
        • @jacek 如果你想正确编码,你需要它。但是如果你想制作脏代码,就像你的情况一样,你不需要它,我同意你的看法。
        【解决方案6】:

        函数 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); //打印数组

        ?>

        【讨论】:

          【解决方案7】:
          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;
          

          }

          【讨论】:

          • 这个修改后的函数接受 $data = 'poison, death, earth' 形式的数据;即用','分隔的字符串,然后它返回一个数组。因此您可以使用 print_r() 打印它或将返回的值视为数组。
          猜你喜欢
          • 2011-04-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-06
          • 2016-01-08
          相关资源
          最近更新 更多