doubi12138 2017-03-28 05:23
浏览 19
已采纳

如何使用for循环获取逗号分隔值和最终值?

PHP code:

$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 . ',';
        }
    }
}

It's result is :

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

But I want remove the final comma and get the last value.

  • 写回答

7条回答 默认 最新

  • douye7033 2017-03-28 05:28
    关注

    Changes done.

    1. Defined $result=array();;

    2. Stored values in an array $result[]= $b;

    3. Imploded an array with , $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?