How can I easily remove the last comma from an arr

2020-02-07 02:09发布

问题:

Let's say I have this:

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

foreach($array as $i=>$k)
{
echo $i.'-'.$k.',';
}

echoes "john-doe,foe-bar,oh-yeah,"

How do I get rid of the last comma?

回答1:

Alternatively you can use the rtrim function as:

$result = '';
foreach($array as $i=>$k) {
    $result .= $i.'-'.$k.',';
}
$result = rtrim($result,',');
echo $result;


回答2:

I dislike all previous recipes.

Php is not C and has higher-level ways to deal with this particular problem.

I will begin from the point where you have an array like this:

$array = array('john-doe', 'foe-bar', 'oh-yeah');

You can build such an array from the initial one using a loop or array_map() function. Note that I'm using single-quoted strings. This is a micro-optimization if you don't have variable names that need to be substituted.

Now you need to generate a CSV string from this array, it can be done like this:

echo implode(',', $array);


回答3:

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);


回答4:

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.



回答5:

I always use this method:

$result = '';
foreach($array as $i=>$k) {
    if(strlen($result) > 0) {
        $result .= ","
    }
    $result .= $i.'-'.$k;
}
echo $result;


回答6:

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++;
}


回答7:

try this code after foreach condition then echo $result1

$result1=substr($i, 0, -1);


回答8:

this would do:

rtrim ($string, ',')


回答9:

see this example you can easily understand

$name = ["sumon","karim","akash"];
foreach($name as $key =>$value){
 echo $value;
 if($key<count($name){
     echo ",";
 }
}


回答10:

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']." , ";
                                  }

                          }