0

I'm creating this JSON thingy and I need to remove the last comma from it. (yes I know I could do a simple way instead of making the json myself, but I need it to be like this {"1":0,"2":4,"3":1.5}) So how can I do it? (And yes I have a working way in the code but it doesent display it like I need it.)

<?php
require 'dbConnect.script.php';

$query="SELECT * FROM `trash`";
if($is_query_run=mysql_query($query)){
    print "{";
    while($query_execute=$query_execute=mysql_fetch_assoc($is_query_run)){

       echo '<tr><td>"'.$query_execute['id'].'"</td>:<td>'.$query_execute['weight'].',</td></tr>';
       //$rows = array();
       //$rows[] = $query_execute;

       //print json_encode($rows);

   }
   print "}";

}

else{

   echo "query notexecuted";

}
?>
9
  • It's useless. Use json_encode. Period. Commented Nov 11, 2016 at 15:40
  • json_encode makes a format that I couldent use therfor I need this :P Commented Nov 11, 2016 at 16:11
  • What you need is to fix the code which can't use standard json_encode output. Commented Nov 11, 2016 at 16:19
  • It's not me that created it so I cannot Commented Nov 11, 2016 at 16:23
  • json_encode displays [{"id":"1","weight":"0"}][{"id":"2","weight":"7"}][{"id":"3","weight":"1.5"}] but I need commas between not ][ Commented Nov 11, 2016 at 16:39

3 Answers 3

1

In your example, you can simply create an array:

$array = ["1" => 0, "2" => 4, "3" => 1.5];
$json = json_encode($array);

Since this array doesn't start with a zero (which indexed arrays does), this would give you your desired result.

If you want to start with a zero, and still get an object back:

$array = ["0" => 2, "1" => 0, "2" => 4, "3" => 1.5];

you can use the option JSON_FORCE_OBJECT as a second parameter, like this:

$array = ["0" => 2, "1" => 0, "2" => 4, "3" => 1.5];
$json = json_encode($array, JSON_FORCE_OBJECT);

This will give you:

{
    "0": 2,
    "1": 0,
    "2": 4,
    "3": 1.5
}

Read more here: http://php.net/manual/en/function.json-encode.php

It's seldom a good idea to build your own encoders/decoders for things like this. It usually gets quite complicated pretty quick, and you will spend most of your time straighten out bugs and get stuck on edge cases. It's better to read up on the native functions. They have been tried and tested for years, and are often much better in regards of performance.

Sign up to request clarification or add additional context in comments.

2 Comments

How should I get the values into the array?
You don't know how to create an array? You should start by reading some basic php tutorials...
0

Whilst I concur json_encode is the nicest solution for producing JSON. To produce a nice comma separated output you could use the implode function.

Set-up your elements in an array, so for example:

$data = array('"1":0', '"2":4', '"3":1.5');

Then use implode like this

$output = implode(',', $data);

Which will give you an output of the data elements in the array as a comma separated list

2 Comments

How should I get the values into the data?
Depends how they are being stored, are they computed or read from a database? If you build a string with the desired element in you can add that to the end of the array with $data[] = $yourString
0
if($is_query_run=mysql_query($query)){ //Here you execute the query
   while($query_execute=$query_execute=mysql_fetch_assoc($is_query_run)){  //Here you get one row from execution result

      $rows = array(); //You create array into $rows, what was in $rows before will be wiped
      $rows[] = $query_execute; // You insert one row you just fetch from result to $rows

      print json_encode($rows); // You encode $rows (which always has only one element as you wipe it every iteration)

  }
}

So, create array before loop, and encode that array after loop, adding elements to array inside a loop is ok. -Mr_KoKa @LinusTechTips.com

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.