Removing last comma in PHP?

前端 未结 7 2107
忘掉有多难
忘掉有多难 2020-11-28 16:36

I have this PHP code:

foreach( $wpdb->get_results(
) as $key => $row) {

echo \"[\'\". $row->DATE . \"\',\". $row->total_sales . \"],\";

}
         


        
7条回答
  •  無奈伤痛
    2020-11-28 16:50

    There are a few approaches:

    1. Push the strings into an array and use implode()
    2. Add the bits to a string, remove the last character, then echo the string
    3. Iterate more explicitly, and on the last iteration, don't output the ','

    Of those options, I'd probably use #1, because it makes the code just a bit more self-documenting.

提交回复
热议问题