【问题标题】:PHP Array output with comma and "or" [duplicate]带有逗号和“或”的PHP数组输出[重复]
【发布时间】:2016-02-17 07:01:42
【问题描述】:

我的 php 输出是一个数组。喜欢:

$array = array('banana', 'mango', 'apple');

现在如果输出只是 'Banana' 那么它将简单地显示

Banana

如果输出是 'banana' & 'mango''apple' 我想显示为

Banana or Mango

如果输出全部.. 则显示结果

Banana, Mango or Apple

现在我可以使用此代码用逗号显示结果

echo implode(",<br/>", $array);

但是如何添加 ??

请帮忙。

【问题讨论】:

  • another post 的相同答案可以应用于此答案。几乎,重复.. 只是 and 而不是 or
  • 这是最简单的方法: $str = implode(', ', array('banana', 'mango', 'apple')); echo substr_replace($str, array(' or'), strrpos($str, ','), 1);
  • 感谢您的评论@Amit Rajput .. 这适用于 3 个或更多数组列表吗?

标签: php arrays


【解决方案1】:

试试这个:

<?php 
$array = array('banana','mango','apple');
$result = '';
$maxelt = count($array) - 1;
foreach($array as $n => $item) {
    $result .= (                           // Delimiter
        ($n < 1) ? '' :                    //    1st?
        (($n >= $maxelt) ? ' or ' : ', ')  //    last or otherwise?
        ) . $item;                         // Item
}
echo $result;

【讨论】:

  • 这是一个非常简单的解决方案,谢谢。 :)
  • 这是最简单的方法: $str = implode(', ', array('banana', 'mango', 'apple')); echo substr_replace($str, array(' or'), strrpos($str, ','), 1);
【解决方案2】:

像这样使用计数和映射

$count = count($your_array);
if($count > 3){
    end($array);         // move the internal pointer to the end of the array
    $key = key($array); // fetches the key of the element pointed to by the internal pointer

    $last = $your_array[$key];

    unset($your_array[$key]);
    echo implode(",<br/>", $your_array)."or"$last;
}

【讨论】:

    【解决方案3】:

    你可以用这个函数替换php中最后一次出现的字符串:

    function str_lreplace($search, $replace, $subject)
    {
        $pos = strrpos($subject, $search);
    
        if($pos !== false)
        {
            $subject = substr_replace($subject, $replace, $pos, strlen($search));
        }
    
        return $subject;
    }
    
    $array = array('banana', 'mango', 'apple');
    
    $output = implode(", ", $array);
    
    echo $output = str_lreplace(',',' or',$output);
    

    【讨论】:

      【解决方案4】:
      $count = count($array);
      
      if( $count == 1 ) {
      
         echo $array[0];
      
      } else if ( $count == 2 ) {
      
         echo implode(' or ', $array);
      
      } else if ( $count > 2 ) {
      
         $last_value = array_pop( $array );
         echo implode(', ', $array).' or '.$last_value;
      
      }
      

      【讨论】:

        【解决方案5】:

        请尝试以下代码:

        $array = array('banana','mango','apple');
        $string = null;
        
        if(count($array) > 1){
        
            $string =  implode(',',$array);
            $pos = strrpos($string, ',');
        
            $string =  substr_replace($string, ' or ', $pos, strlen(','));
        }elseif(count($array) == 1){
            $string =  $array[0];
        }
        echo $string;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-29
          • 2015-04-11
          相关资源
          最近更新 更多