-1

I am trying to strip the last , from a string but do not succeed in it. I need a string like this: 2,3,4 But when trying to strip the last , i get as ouput: 234 instead of 2,3,4

$alldays = array('2','3','4');

foreach($alldays as $day) {
    $string = $day.',';
    $string_without_last_comma = substr($string, 0, -1);
    echo $string; // output: 2,3,4,
    echo $string_without_last_comma; // output: 234 ( should be 2,3,4)

}

How can I achieve this?

0stone0
  • 34,288
  • 4
  • 39
  • 64
john
  • 1,263
  • 5
  • 18
  • 3
    Hint: [`implode`](https://www.php.net/manual/en/function.implode.php). – El_Vanja Apr 27 '21 at 13:32
  • 2
    `$string` only exists inside the loop, so you're adding the comma, and removing at next line. Wouldn't `implode` be a much better solution? – 0stone0 Apr 27 '21 at 13:33
  • Yes, `implode` exists, but for more general cases: Don’t _add_ the extra comma in the first place! That becomes easier, if you turn the logic around - don’t add a comma _after_ each item, but _before_. Then you only have to handle the case of the first item - which you can then easily do, by checking if `$string` is still empty. If it is, then you are about to add the first item to it, and therefor don’t need to add a comma before it. – CBroe Apr 27 '21 at 13:35
  • I agree. `implode` fixes this problem. thnx – john Apr 27 '21 at 13:40

1 Answers1

3

Answer

Use implode()

<?php
$alldays = array('2','3','4');
$string = implode(',', $alldays);
echo $string;

Try it online!

Debug

Let's walk though the code

<?php
// Define alldays
$alldays = array('2','3','4');

// For each day
foreach($alldays as $day) {

    // Create(=) a string, add $day and a comma
    $string = $day.',';

    // Remove the last char from $string and save ('NOT ADD') in $string_without_last_comma
    $string_without_last_comma = substr($string, 0, -1);

    // Show string
    echo $string;

    // Show string with last char
    echo $string_without_last_comma;
}

// String here is the last char?
echo $string;

So the loop does show all those values, but they're not being added to eachother, only shown once per loop iteration.

Recap;

  1. Since you're doing $string = $day.','; you're overriding $string every loop
  2. Same for $string_without_last_comma; you're not appending anything, only overriding
  3. implode() would give the desired result

Fixed original code

Note: Pure for learning purposes, I'll still recommend implode().

Without the use of implode(), my guess is that you're trying to do something like this;

<?php

// Define alldays
$alldays = array('2','3','4');

// Create empty string
$string = '';

// For each day
foreach($alldays as $day) {

    // Add(.=) $day and a comma
    $string .= $day . ',';

    // Keep doing this logic for all the $day's in $alldays
}

// Now $string contains 2,3,4,
// So here we can create $string_without_last_comma by removing the last char
$string_without_last_comma = substr($string, 0, -1);

// Show result
echo $string_without_last_comma;

Here we

  1. Create an empty string
  2. Loop through all the days
  3. Add (.=) day to $string and add a comma
  4. After the loop, we can remove the last comma
  5. Show the result

Try it online!

0stone0
  • 34,288
  • 4
  • 39
  • 64