Hello there!

Is it possible to change the last result in a foreach iteration?

Im using Google Chart to visualize some data, the data itself comes from a foreach loop, like this:

foreach ($this->dados as $dado) :

    $media = $this->escape($dado->media);
echo $media.',';

endforeach;	

This code gives me values like "10.37," which is fine, since the values are separated by commas in Google Chart, the problem is that the last value can not contain a comma, otherwise it wont work. Is there some way that i can remove it from the last result/value?

Thanks in advance!

    I would concatenate the string and substr the last ,

    foreach ($this->dados as $dado) :
    
        $media .= $this->escape($dado->media).',';
    
    endforeach;
    echo substr( $media , 0 , -1 );
    

      Worked like a charm.

      Thank you!

        Write a Reply...