Create a comma-separated string from a single column of an array of objects

前端 未结 14 1323
醉话见心
醉话见心 2020-11-30 07:36

I\'m using a foreach loop to echo out some values from my database, I need to strip the last comma from the last loop if that makes sense.

My loop is just simple, as

相关标签:
14条回答
  • 2020-11-30 07:58
    <?php
    $i = 1;
    $count = count( $results );
    foreach( $results as $result ) {
        echo $result->name;
        if ( $i < $count ) echo ", ";                               
        ++$i;
    }
    ?>
    
    0 讨论(0)
  • 2020-11-30 08:01

    First get all the output by using output buffering. Then, trim the comma and display it. So, do it like this:

    ob_start();
    foreach($results as $result)
    {
       echo $result->name.',';
    }
    $output = ob_get_clean();
    
    echo rtrim($output, ',');
    

    The output buffering method helps if the inside loop is very big (and OP is posting here just for brevity), then using OB is easier without changing the internals of the loop.

    0 讨论(0)
  • 2020-11-30 08:01

    1. Concat to string but add | before

    $s = '';
    foreach ($results as $result) { 
        if ($s) $s .= '|';
        $s .= $result->name; 
    }
    echo $s;
    

    2. Echo | only if not last item

    $s = '';
    $n = count($results);
    foreach ($results as $i => $result) { 
        $s .= $result->name;
        if (($i+1) != $n) $s .= '|';
    }
    echo $s;
    

    3. Load to array and then implode

    $s = array();
    foreach ($results as $result) { 
        $s[] = $result->name;
    }
    echo implode('|', $s);
    

    4. Concat to string then cut last | (or rtrim it)

    $s = '';
    foreach ($results as $result) { 
        $s .= $result->name . '|';
    }
    echo substr($s, 0, -1); # or # echo rtrim($s, '|');
    

    5. Concat string using array_map()

    echo implode('|', array_map(function($result) { return $result->name; }, $results));
    
    0 讨论(0)
  • 2020-11-30 08:01

    I have to do this alot because I'm always trying to feed numbers in to jplot, I find its easier to put the comma in the front of the loop like so:

    foreach($arrayitem as $k){ $string =  $string.",".$k;
     } 
    

    and then chop off the first character (the comma) using substr, it helps if you know a guestimate of long your string will be, I'm not sure what the limit on substr max character is.

     echo substr($a,1,10000000);
    

    hope this helps.

    0 讨论(0)
  • 2020-11-30 08:04

    I've been having the same issue with this similar problem recently. I fixed it by using an increment variable $i, initializing it to 0, then having it increment inside the foreach loop. Within that loop place an if, else, with the echo statement including a comma if the $i counter is less than the sizeof() operator of your array/variable.

    I don't know if this would fix your issue per se, but it helped me with mine. I realize this question is years-old, but hopefully this will help someone else. I'm fairly new to PHP so I didn't quite understand a lot of the Answers that were given before me, though they were quite insightful, particularly the implode one.

    $i=0;
    foreach ($results as $result) {
        $i++;
        if(sizeof($results) > $i) {
            echo $result . ", ";
        } else {
            echo $result;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 08:04

    This is what I normally do, add a comma before the item rather than after, while ignoring the first loop.

    $i = 0;
    $string = '';
    
    foreach($array as $item){
        $string .= ($i++ ? ',' : '').$item;
    }
    
    0 讨论(0)
提交回复
热议问题