PHP comma separated array?

I have a query that generates TIDs but since it’s in the loop the result shows like this: 44214420441944184416

I turn in into an array:

Array
(
    [0] => 4421
)
Array
(
    [0] => 4420
)
Array
(
    [0] => 4419
)
Array
(
    [0] => 4418
)
Array
(
    [0] => 4416
)

With this code:

$test = $row['tid'];
         $results = array();
         $results[] = $test;
         
         print_r($results);

How to output those numbers comma separated like so: 4421,4420,4419,4418,4416

I’d suggest changing the code in your loop slightly, so instead of $results[] = $test; you’d do $results[] = $test[0]; which would give you an array like this:

Array
(
    [0] => 4421
    [1] => 4420
    [2] => 4419
    [3] => 4418
    [4] => 4416
)

Then you can call implode(',', $results); which will give you the comma-separated list of IDs.

Thanks buddy but some is wrong:

$test = $row['tid'];

        $results[] = $test[0];
        
        print_r($results);

returns this:

Array
(
    [0] => 4
)
Array
(
    [0] => 4
    [1] => 4
)
Array
(
    [0] => 4
    [1] => 4
    [2] => 4
)
Array
(
    [0] => 4
    [1] => 4
    [2] => 4
    [3] => 4
)
Array
(
    [0] => 4
    [1] => 4
    [2] => 4
    [3] => 4
    [4] => 4
)

Your result suggests that $test is what you wanted to begin with.

$results = array(); //This bit is important so that $results is available.
while(...) {
//...
$results[] = $row['tid']; //There is no point in $test, unless you're doing something else with it.
//...
}
echo implode(",",$results);
2 Likes

Perfect thanks a lot works awesome :smiley:

I am not sure why it generates this:

44214421,44204421,4420,44194421,4420,4419,44184421,4420,4419,4418,4416

Got echo implode(“,”,$results); has to be outside the loop.

MARK AS SOLVED!

Whoops, I misread the code in the OP. Need more coffee, I think! :coffee:

1 Like

Guys still need your help! The implode doesn’t work inside the loop how to make it work?

while($row = $db->fetch_array($query))
        {

            $results = array();
                
            $results[] = $row['tid'];
 
            echo implode(",", $links);
}

It has to be inside as my code is located inside the while loop.

Try this:

$xx = '44214420441944184416';
$cnt=1;
for($i2=0; $i2<strlen($xx); $i2++):
  echo $xx["$i2"];
  if( $cnt % 4 ):
    //
  else:
    echo ', ';
  endif;
  $cnt++;
endfor;

Your code should be before, inside, and after the while loop, as was shown to you. You cannot create and echo a string completely inside the loop without having a dangling comma.

Can I create 2 while for 1 query? The one will be only to generate array?

So… you’re telling me you cant write

but you can write
while(…) {
$results = $row[‘tid’];
}
foreach($results AS $result) {

}

???

I dont understand your restrictions.

Thank you all! I simply crated new query that will generate array and use in the other loop query.

Thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.