How to send an array of objects in JSON format from PHP

New in php. I am trying to send JSON data to the front end in a name-value pair. I tried the example I got here The following is a snippet of code that sends data in the format of a JSON name value.

while($stmt->fetch()){ $list = array('id' => $fid, 'name' => $fname); } $stmt->free_result(); $stmt->close(); echo json_encode($list); 

I got this on the interface

 Object {id: 12, name: "Manisha"} 

The problem is that I was expecting an array of objects. The above value is the last value obtained from the SQL query. What changes should I make to this code so that I can get an array of objects. Sort of

 [{"id":"1","name":"Kumari"}, {"id":"2","name":"KM"}, {"id":"3","name":"Manisha"}] 

Please advice.

+6
source share
6 answers

$ list should be an array, and you can just click elements on it, as in this code:

 $list = array(); while($stmt->fetch()){ $list[] = array('id' => $fid, 'name' => $fname); } $stmt->free_result(); $stmt->close(); echo json_encode($list); 

You can also use the fetch_all () method to immediately get all the rows, not iterate through a loop. Although in this example you would get all the fields that you selected, instead of a simple name and a name.

 $list = $stmt->fetch_all(); $stmt->free_result(); $stmt->close(); echo json_encode($list); 
+14
source

You should try to push each object at the end of the array, for example, add to the stack using array_push (equivalent to $array[] = $data , but makes it more readable for you).

 $list=array(); //instantiate the array while($stmt->fetch()){ $data = new stdClass(); // create a new object $data->id=$fid; $data->name=$fname; array_push($list,$data); // push object to stack array } $stmt->free_result(); $stmt->close(); echo json_encode($list); 
+3
source

Your list array only stores 1 row. Try the following:

 while ($stmt->fetch()) { $list[] = array('id' => $fid, 'name' => $fname); } 

Hope this helps!

+1
source

See if this works for you:

 $n = 0; while($stmt->fetch()){ $list[$n] = array('id' => $fid, 'name' => $fname); $n++; } $stmt->free_result(); $stmt->close(); echo json_encode($list); 

You rewrote $list several times with an array.

0
source

try to create an array of objects

 $list = array(); while($stmt->fetch()) { // create an object $datum=new stdClass(); $datum->id=$fid; $datum->name=$fname; $list[] = $datum; } $stmt->free_result(); $stmt->close(); echo json_encode($list); 
0
source

Suppose we have this file structure with no specific objects inside:

  { "Subjects" : [] } //Get your Json file and decode it $json = file_get_contents('CdStore/Pagetest.json'); $json_data = json_decode($json,true); //Specifiy your Objects like this $NewArray= array( 'Mathematics' => array(),'Physics' => array()); //Push the new Objects array_push($json_data['Pagess'],$NewArray); //Then encode to json $array = json_encode($json_data); //If you want to get the preview before saving print_r($array); //Save your file file_put_contents('CdStore/Pagetest.json', json_encode($json_data)); 

As a result, you have:

  {"Subjects": [ { "Mathematics":[], "Physics":[] } ] } 
0
source

All Articles