"doe", "foe" => "bar", "oh" => "yeah"); for...">

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

Let's say I have this:

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

echo signals "john-doe,foe-bar,oh-yeah,"

How do I get rid of the last comma?

+6
string arrays php
source share
10 answers

Alternatively, you can use rtrim like:

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

I do not like all the previous recipes.

Php is not C and has higher level methods to solve this particular problem.

I will start from the moment when you have such an array:

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

You can build such an array from the original using the loop function or array_map () . Note that I use single-quoted strings. This is micro-optimization if you do not have variable names to replace.

Now you need to create a CSV string from this array, this can be done as follows:

 echo implode(',', $array); 
+10
source share

One method is to use 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 to use implode

 $array = array("john" => "doe", "foe" => "bar", "oh" => "yeah"); $output = array(); foreach($array as $i=>$k) { $output[] = $i.'-'.$k; } echo implode(',', $output); 
+7
source share

I don't like this idea of ​​using substr in general, as it is a bad programming style. The idea is to combine all the elements and separate them with special "separation" phrases. The idea of ​​naming a substring for this is similar to using a laser to take pictures of birds.

In the project that I am currently facing, we are trying to get rid of bad habits in coding. And this pattern is considered one of them. We force programmers to write this code as follows:

 $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 just use the implode function (our project is in Java, so we had to create our own function to concatenate strings this way). You should use the substr function only when you have a real need for it. This should be avoided here, as it is a sign of a bad programming style.

+3
source share

I always use this method:

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

Assuming the array is an index, this works for me. I loop $ i and test $ i against the $ key. When the key ends, the commas do not print. Note that IF has two values ​​to make sure that 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++; } 
+1
source share

try this code after foreach condition then echo $ result1

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

this would do:

 rtrim ($string, ',') 
0
source share

see this example you can easily understand

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

I removed the comma from the last aray value using the last key of the array. Hope this gives you an 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
source share

All Articles