-1

I've been trying to figgure out using substr, rtrim and it keeps removing all the commas. And if it doesn't nothing shows up. So I am basicly stuck and require some help.. Would've been apriciated.

        if(is_array($ids)) {
        foreach($ids as $id) {
            $values = explode(" ", $id);
            foreach($values as $value) {
                $value .= ', ';
                echo ltrim($value, ', ') . '<br>';

            }
        }
    }
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63

4 Answers4

2

I am guessing that you are trying to take an array of strings of space separated ids and flatten it into a comma separated list of the ids.

If that is correct you can do it as:

$arr = [
    'abc def ghi',
    'jklm nopq rstu',
    'vwxy',
];
$list = implode(', ', explode(' ', implode(' ', $arr)));
echo $list;

output:

abc, def, ghi, jklm, nopq, rstu, vwxy
vladsch
  • 606
  • 6
  • 7
  • You're welcome and to answer your second question: change 'where id = ({$ids)' to 'where id in ({$ids})' equal is only for equality. Use 'in' to test for presence in a list. – vladsch Mar 06 '15 at 04:34
0

Change ltrim by rtrim:

ltrim — Strip whitespace (or other characters) from the beginning of a string

rtrim — Strip whitespace (or other characters) from the end of a string

<?php

$ids = Array ( 1,2,3,4 ); 
$final = '';

        if(is_array($ids)) {
        foreach($ids as $id) {
            $values = explode(" ", $id);
            foreach($values as $value) {
                $final .= $value . ', ';
            }
            
        }
        
        echo rtrim($final, ', ') . '<br>';
        echo substr($final, 0, -2) . '<br>'; //other solution
    }
?>
Community
  • 1
  • 1
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
  • are you sure that you copied all closing } ? this code should work only with an extra
    in the end.
    – kos Mar 06 '15 at 03:30
0

Use trim() function.

Well, if you have a string like this

 $str="foo, bar, foobar,";

use this code to remove the Last comma

<?Php
$str="foo, bar, foobar,";
$string = trim($str, " ,");
echo $string;

output: foo, bar, foobar

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
0

If your array looks like;

[0] => 1,
[1] => 2,
[2] => 3,
...

The following should suffice (not the most optimal solution);

$string = '';  // Create a variable to store our future string.
$iterator = 0; // We will need to keep track of the current array item we are on.

if ( is_array( $ids ) ) 
{
   $array_length = count( $ids ); // Store the value of the arrays length

   foreach ( $ids as $id ) // Loop through the array
   {
      $string .= $id; // Concat the current item with our string

      if ( $iterator >= $array_length ) // If our iterator variable is equal to or larger than the max value of our array then break the loop.
        break;

      $string .= ", "; // Append the comma after our current item.
      $iterator++; // Increment our iterator variable

   }
}

echo $string; // Outputs "1, 2, 3..."
dojs
  • 497
  • 2
  • 11