How can I easily remove the last comma from an array?

前端 未结 10 1128
予麋鹿
予麋鹿 2020-12-04 01:50

Let\'s say I have this:

$array = array(\"john\" => \"doe\", \"foe\" => \"bar\", \"oh\" => \"yeah\");

foreach($array as $i=>$k)
{
echo $i.\'-\'.$         


        
10条回答
  •  误落风尘
    2020-12-04 02:35

    I have removed comma from last value of aray by using last key of array. Hope this will give you idea.

       $last_key =  end(array_keys($myArray));
                               foreach ($myArray as $key => $value ) {
                                  $product_cateogry_details="SELECT * FROM `product_cateogry` WHERE `admin_id`='$admin_id' AND `id` = '$value'";
                                      $product_cateogry_details_query=mysqli_query($con,$product_cateogry_details);
                                      $detail=mysqli_fetch_array($product_cateogry_details_query);
    
                                      if ($last_key == $key) {
                                        echo $detail['product_cateogry'];
                                      }else{
                                        echo $detail['product_cateogry']." , ";
                                      }
    
                              }
    

提交回复
热议问题