Remove last comma from an array

Asked

Viewed 6,566 times

10

In this Code below I have a array that returns a comma to me at the end exe: '98602','98603','98604',

How can I remove this comma?

 if (isset($_POST['checkbox'])) {
 foreach ($_POST['checkbox'] as $key => $value) {
    $id = mysql_real_escape_string($value);

    $regra = "'{$id}',";
    echo $regra;       
 }}
  • 2

    It is not the array that returns the comma at the end, my friend. It is the string you are using

5 answers

13


After the foreach use replace to remove the last character:

If last parameter(lenght) is negative it will remove the characters at the end of the string. Don’t forget to start $regra otherwise its value will always be 'reset' with each round of the.

 if (isset($_POST['checkbox'])) {
    $regra = '';
    foreach ($_POST['checkbox'] as $key => $value) {
      $id = mysql_real_escape_string($value);

      $regra .= "'{$id}',";

    }
    $regra = substr($regra, 0, -1);
    echo $regra;
 }

Another way to format this string at once is to combine the first and last simple quote between implode call:

$arr = array('98602','98603','98604'); // equivalente o $_POST
$novo = "'". implode("','", $arr) ."'";
echo 'implode '. $novo;

Exit:

'98602','98603','98604'
  • I just hadn’t used implode because of the escape it makes in the string before, but it’s an interesting alternative to simpler cases than the question.

  • @Bacco, I’m testing with array_map to call the mysql_real_escape_string

9

There are several ways, even you can make an algorithm that is not inserted. I think the easiest would be the rtrim():

if (isset($_POST['checkbox'])) {
    foreach ($_POST['checkbox'] as $key => $value) {
        $id = mysql_real_escape_string($value);
        $regra = "'{$id}',";
        echo $regra;       
    }
    rtrim($regra, ',');
}

I put in the Github for future reference.

6

Based on what @Maniero mentioned, one way is not to insert the comma, instead of removing it. Note that the code gets longer than with rtrim:

$regra = '';
$separador = '';
if ( isset( $_POST['checkbox'] ) ) {
   foreach ( $_POST['checkbox'] as $value ) {
      $regra .= $separador."'".mysql_real_escape_string( $value )."'";
      $separador = ',';
   }
}
echo $regra;       

I took the opportunity to give a little optimized taking the echo of the main logic and removing the $key => and the $id which are not necessary in this case.

6

You can map this array and then merge the array elements with commas in the middle:

$func = function($value) {
     $id = mysql_real_escape_string($value);
     return "'{$id}'";
};

$ids = array_map($func, $_POST['checkbox']);
echo implode(",", $ids);

You have other correct answers, this is another approach I prefer.
The if (isset($_POST['checkbox'])) it’s still necessary, that part you can keep.

  • 1

    I’m sorry, I didn’t see you had answered that already! + 1

-2

Another way to solve it would also be

if (isset($_POST['checkbox'])) {
  $regra = "";
  foreach ($_POST['checkbox'] as $key => $value) {
    $id = mysql_real_escape_string($value);

    $regra .= ($regra != "" ? "," : "") . "'{$id}'";
  }
 }
  • 2

    Hello. The second half of the reply has been removed as this is considered spam. If you want, feel free to promote your company or product on your own user profile. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.