php - How to echo JSON doubly nested object literal w/ proper "headers" -


i'm trying echo json object literal includes every row in database table.

as standard methods found in many other similar questions looked at, i'm fetching every row , encoding so:

$con=mysqli_connect("localhost","username","pass","markers");//markers db table name $result = mysqli_query($con,"select * markers");  $rows = array();  while($r = mysqli_fetch_array($result))   {        $rows[] = $r        //or: $rows[] = array('data' => $r);   }  echo json_encode($rows); 

similarly question php: can't encode json multiple rows or mysql table json ,

i want echo json object looks this:

    {     "data1":   // how "data1"?         {             name: john smith,             title: mr,             description: man         }     }      {     "data2":{ // how "data2"?              name:bob smith,             title: mr,             description: guy         }      } 

except not how achieve "headers", titles of first level string of objects, such "data1" or data2". in fact, database table doesn't have values/that column.

this looks right now: database table

how can numerical "headers" "1", "2" or "data1", "data2" without having designate "name" column "headers"? (or have have column that?)

my goal process values in every "row" in returned json.

i plan use jq $.each function $.each(data, function(datanum, datainfo) -- e.g. data1 passed datanum , values passed datainfo -- , able access specific table values datainfo.name, right not working.

thanks in advance!

i think way better off using mysqli_fetch_object() each row own object. when json_encode $rows have array of objects.

here code:

$con=mysqli_connect("localhost","username","pass","markers");//markers db table name $result = mysqli_query($con,"select * markers");  $rows = array();  while($r = mysqli_fetch_object($result))   {        $rows[] = $r;   }  echo json_encode($rows); 

your json this:

[     {         "name":"some name",         "title":"some title",         "description":"some description"     },     {         "name":"some other name",         "title":"some other title",         "description":"some other description"     },     ... ] 

in javascript, gives integer-indexed array of objects. javascript might this:

var array = json.parse(jsonstring); $.each(array, function(key, value) {     alert(key); // numerical index     alert(value.name); // name     alert(value.title); // title     alert(value.description); // description }); 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -