Removing last comma in PHP?

前端 未结 7 2035
忘掉有多难
忘掉有多难 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 17:08
    $numItems = count($wpdb->get_results());
    $i = 0;
    
    foreach( $wpdb->get_results() as $key => $row) {
    if($i+1 == $numItems) {
        echo "['". $row->DATE . "',". $row->total_sales . "]";
      }
    else{
    echo "['". $row->DATE . "',". $row->total_sales . "],";
    
    }
      $i++;
    }
    
    0 讨论(0)
提交回复
热议问题