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

前端 未结 10 1031
予麋鹿
予麋鹿 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:22

    try this code after foreach condition then echo $result1

    $result1=substr($i, 0, -1);
    
    0 讨论(0)
  • 2020-12-04 02:22

    see this example you can easily understand

    $name = ["sumon","karim","akash"];
    foreach($name as $key =>$value){
     echo $value;
     if($key<count($name){
         echo ",";
     }
    }
    
    0 讨论(0)
  • 2020-12-04 02:32

    Assuming the array is an index, this is working for me. I loop $i and test $i against the $key. When the key ends, the commas do not print. Notice the IF has two values to make sure the first value does not have a comma at the very beginning.

    foreach($array as $key => $value)
    {
        $w = $key;
        //echo "<br>w: ".$w."<br>";// test text
        //echo "x: ".$x."<br>";// test text
        if($w == $x && $w != 0 )
        {
        echo ", ";
        }
        echo $value;
        $x++;
    }
    
    0 讨论(0)
  • 2020-12-04 02:33

    One method is by using substr

    $array = array("john" => "doe", "foe" => "bar", "oh" => "yeah");
    
    $output = "";
    foreach($array as $i=>$k)
    {
        $output .= $i.'-'.$k.',';
    }
    
    $output = substr($output, 0, -1);
    
    echo $output;
    

    Another method would be using implode

    $array = array("john" => "doe", "foe" => "bar", "oh" => "yeah");
    
    $output = array();
    foreach($array as $i=>$k)
    {
        $output[] = $i.'-'.$k;
    }
    
    echo implode(',', $output);
    
    0 讨论(0)
  • 2020-12-04 02:34

    I don't like this idea of using substr at all, since it's the style of bad programming. The idea is to concatenate all elements and to separate them by special "separating" phrases. The idea to call the substring for that is like to use a laser to shoot the birds.

    In the project I am currently dealing with, we try to get rid of bad habits in coding. And this sample is considered one of them. We force programmers to write this code like this:

    $first = true;
    $result = "";
    foreach ($array as $i => $k) {
      if (!$first) $result .= ",";
      $first = false;
      $result .= $i.'-'.$k;
    }
    echo $result;
    

    The purpose of this code is much clearer, than the one that uses substr. Or you can simply use implode function (our project is in Java, so we had to design our own function for concatenating strings that way). You should use substr function only when you have a real need for that. Here this should be avoided, since it's a sign of bad programming style.

    0 讨论(0)
  • 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']." , ";
                                      }
    
                              }
    
    0 讨论(0)
提交回复
热议问题