0

I have problems because I can't delete last, from my echo

with rtrim

  while($t= mysql_fetch_array($asd)){
    $rest = '
       "'.$t['a'].'": "'.$t['b'].'",';
    echo rtrim($rest,",");

output:

   "1": "a"
   "2": "b"
   "3": "a"
   "4": "b"

how I can achieve like this:

output:

   "1": "a",
   "2": "b",
   "3": "a",
   "4": "b"

I arleady trying with some rtrim,explode etc but its doesnt work

I arleady trying with rtrim this doesnt work here is my code:

 while($t= mysql_fetch_array($asd)){
$str = '
   "'.$t['a'].'": "'.$t['b'].'",';
    echo rtrim($str, ",");
Dinidu Hewage
  • 2,169
  • 6
  • 40
  • 51

6 Answers6

3

From what i see is that you are trying to generate a json object, this is not the way to do that.

$data = [];
while (($t = mysql_fetch_array($asd))) {
    $data[$t['a']] = $t['b'];
}

echo json_decode($data);

This will output a json string in the format you are trying to generate.


Sidenote 1:

The reason your rtrim in your code is not working is because you rtrim every line in the while loop, not the last line. But since you use $rest = you are also overwriting the value of $rest in every iteration.


Sidenote 2:

What @jeroen is saying in the comments, do not use mysql_fetch_array, these methods have been deprecated for some time. Look for PDO, mysqli or use a php framework with a proper ORM.

Robert
  • 5,703
  • 2
  • 31
  • 32
1

Besides rtrim you can use substr() to remove the last character:

while ($something) {
    //construct $str
}
$str = substr($str, 0, -1);
danopz
  • 3,310
  • 5
  • 31
  • 42
1

You have to do on single string, but right now you are doing it on every string instance in while loop,

$str = '';
while($t= mysql_fetch_array($asd)){
   $str .= '
   "'.$t['a'].'": "'.$t['b'].'",';

}

$str = rtrim($str,",");

Give it a try, this will help.

Rahul
  • 18,271
  • 7
  • 41
  • 60
0

use php rtrim() function to remove the , from the right. The rtrim() function removes whitespace or other predefined characters from the right side of a string.

echo rtrim($rest,",");//remove all the , from right

or, use substr() function of php to remove the last character

echo substr($rest,0,strlen($rest)-1);// remove the last character only
RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70
0

try this

$rest = '';
while($t= mysql_fetch_array($asd)){
  $rest .= ' "'.$t['a'].'": "'.$t['b'].'",';  
}

$rest = rtrim($rest,",");
echo $rest;
Charly
  • 101
  • 1
  • 8
0

Just to throw in an alternative solution:
You could not attach the trailing comma in the first place instead of trimming it afterwards.

Let's assume you have the number of results in $num_rows, then you could do:

$i = 1;
while($t = mysql_fetch_array($asd)) {
    $rest = '"' . $t['a'] . '": "' . $t['b'] . (($i==$num_rows) ? '"' : '",');
    ++$i;
}
echo $rest;

Of course, note what some other answers pointed out: your echo was in the loop, but should stand outside of the loop.

Please also note that you are using the deprecated mysql extension; you should use mysqli instead. See the note in the PHP docs:

Warning This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information.

domsson
  • 4,553
  • 2
  • 22
  • 40