【问题标题】:How to get comma separated values and final value using a for loop?如何使用 for 循环获取逗号分隔值和最终值?
【发布时间】:2019-07-05 20:56:24
【问题描述】:

PHP 代码:

$b = 1;
$ab = 100;

for ($b; $b < $ab; $b++)
{
    $len = strlen($b);
    $c = 0;
    for ($c; $c < $len; $c++)
    {
        $split = str_split($b);

        if ($split[$c] == 5)
        {
            echo $b . ',';
        }
    }
}

结果是:

 5,15,25,35,45,50,51,52,53,54,55,55,56,57,58,59,65,75,85,95,

但我想删除最后的逗号并获取最后一个值。

【问题讨论】:

  • 使用 echo ltrim(','.$b) 或 rtrim(',',$b);
  • 存储在数组中然后使用implode()

标签: php loops csv concatenation filtering


【解决方案1】:

已完成更改。

1.定义$result=array();

2.将值存储在数组$result[]= $b;

3. 使用, $result= implode(",", $result); 内爆数组

PHP code demo

<?php

$b = 1;
$ab = 100;
$result=array();

for ($b; $b < $ab; $b++)
{
    $len = strlen($b);
    $c = 0;
    for ($c; $c < $len; $c++)
    {
        $split = str_split($b);

        if ($split[$c] == 5)
        {
            $result[]= $b;
        }
    }
}
$lastElement =end($result);//last element
$result=  implode(",", $result);
print_r($result);

【讨论】:

  • 还添加一些说明,对以后的访问者有帮助
【解决方案2】:

有两种方法。要么使用数组中的所有值,要么将字符串 explode 放入 array 第一种方法是

$b = 1;
    $ab = 100;
    $arr=[];

    for($b; $b < $ab; $b++){
        $len = strlen($b);
        $c = 0;
        for($c; $c < $len; $c++){
            $split = str_split($b);

            if($split[$c] == 5){
                $arr[]=$b;//push value into the array
            }
       }  
    }
     echo implode(",",$arr);//create string from array
    echo end($arr);// return the last value of a array

第二种方式是

$b = 1;
    $ab = 100;
    $str="";

    for($b; $b < $ab; $b++){
        $len = strlen($b);
        $c = 0;
        for($c; $c < $len; $c++){
            $split = str_split($b);

            if($split[$c] == 5){
              $str .=$b .','; //create a string with name str
            }
       }  
    }
    $str=rtrim($str,','); //remove last comma of this string
    echo $str;
    $arr=explode(",",$str);//convert string to array
    echo end($arr);//return the last value of this array 

【讨论】:

    【解决方案3】:

    其他所有答案的效率都太低了,因为他们在内部 for() 循环中使用 str_split() 来构建数组。

    每次都用逗号连接您的值并要求您编写一个单独的条件来处理逗号。这只会使您的代码混乱。最佳做法是将所有值存储在一个数组中,然后使用implode() 将它们与逗号粘合在一起——这样可以避免不需要的尾随逗号。 end() 是提取最后一个值的最佳方式。

    这是一种更快的方法(因为它避免了不必要的循环和函数调用)来动态构建数组,将其转换为 csv 字符串,并访问数组中的最新元素:

    代码:

    $d="5";                          // must be declared as string for strpos()
    $x11=$d*11;                      // set only duplicate occurrence in range
    for($x=1; $x<100; ++$x){
        if(strpos($x,$d)!==false){   // if at least one $d is found
            $array[]=$x;
            if($x==$x11){            // if two $d are found in integer
                $array[]=$x;
            }
        }
    }
    echo implode(',',$array),"<br>Last number: ",end($array);  // display
    

    输出:

    5,15,25,35,45,50,51,52,53,54,55,55,56,57,58,59,65,75,85,95
    Last number: 95
    

    此方法进行 99 次迭代、99 次 strpos() 调用和 20 次比较以达到所需的结果。 将此与此页面上的所有其他代码进行比较,这些代码进行了 99 次外循环迭代、99 次 strlen() 调用、189 次内循环迭代、189 次 str_split() 调用和 189 次比较。


    *faster 仍然是删除 strpos() 块内的比较,将 x11 值添加到循环外的数组中,然后在显示之前对数组进行排序。像这样:

    代码:

    $d="5";                          // must be declared as string for strpos()
    $array[]=$d*11;                  // store only duplicate occurrence in range
    for($x=1; $x<100; ++$x){
        if(strpos($x,$d)!==false){   // if at least one $d is found
            $array[]=$x;
        }
    }
    sort($array);
    echo implode(',',$array),"<br>Last number: ",end($array);  // display
    

    这是使用函数迭代的替代方法:

    代码:

    $d=5;
    $array=array_filter(range(1,99),function($v)use($d){return strpos($v,(string)$d)!==false;})+[$d*11];
    sort($array);  // zero-index the keys and position the appended value
    echo implode(',',$array),"<br>Last number: ",end($array);  // display
    

    输出:

    5,15,25,35,45,50,51,52,53,54,55,55,56,57,58,59,65,75,85,95
    Last number: 95
    

    【讨论】:

      【解决方案4】:

      使用 rtrim 然后分解得到一个数组。

       $b = 1;
       $ab = 100;
      
       for($b; $b < $ab; $b++){
          $len = strlen($b);
          $c = 0;
          for($c; $c < $len; $c++){
             $split = str_split($b);
      
             if($split[$c] == 5){
                echo $b .','; 
             }
          }  
       }
      
       $b = rtrim(',',$b);
       $b = explode(',',$b);
       $b = $b[count($b)-1];
      

      【讨论】:

      • 把我的答案贴在下面
      【解决方案5】:

      这将删除最后一个逗号并为您提供最后一个值(作为字符串)。您可以随时将字符串改回整数。

      $b = 1;
      $ab = 100;
      $string = "";
      
      for($b; $b < $ab; $b++){
          $len = strlen($b);
          $c = 0;
          for($c; $c < $len; $c++){
              $split = str_split($b);
      
              if($split[$c] == 5){
                 $string .= $b .','; 
              }
         }  
      }
      
      $string = trim($string,",");
      $pos = strripos($string,",");
      echo substr($string,$pos + 1);
      

      【讨论】:

        【解决方案6】:

        只需定义最后一个变量:

        $b = 1;
        $ab = 100;
        $last = 0;
        for($b; $b < $ab; $b++){
            $len = strlen($b);
            $c = 0;
            for($c; $c < $len; $c++){
                $split = str_split($b);
        
                if($split[$c] == 5){
                   if($last > 0){
                       echo ',';
                   }
                   echo $b; 
                   $last = $b;
                }
           }  
        }
        

        每次将最后一个值放入$last

        对于 coma,它会检查 $last &gt; 0 是否将 coma 放在 $b 之前

        【讨论】:

          【解决方案7】:

          您也可以使用这些代码。测试了一下,效果不错。

          $b = 1;
          $ab = 100;
          $hasValue = false;
          for($b; $b < $ab; $b++){
              $hasFive = false;
              $len = strlen($b);
              $c = 0;
              for($c; $c < $len; $c++){
                  $split = str_split($b);
          
                  if($split[$c] == 5){
                      $hasFive = true; 
                  }
              } 
          
            if (!$hasValue && $hasFive) {
                echo $b;
                $hasValue = true;
             }else if($hasFive){
                echo ','.$b;
             }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-05-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-04-24
            • 2021-06-17
            • 1970-01-01
            相关资源
            最近更新 更多