php - Create Nested Array From Mysql -


i trying proper json data mysql. made lot of progress. take look;

i have table below:

name             folder    path raymala          787       01.jpg,02.jpg,03.jpg,04.jpg,05.jpg... raymala          788       01.jpg,02.jpg,03.jpg,04.jpg,05.jpg,06.jpg... falitiko         332       01.jpg... falitiko         333       01.jpg,02.jpg... 

my current code:making array single table cell.

$rows = array(); while($r = mysqli_fetch_assoc($result)) {        $rows[] = $r; }  $narray = array(); foreach($rows $value){     $narray[] = array('name' => $value['name'], 'folder' => $value['folder'], 'path' => explode(",", $value['path'])); }  print json_encode($narray); 

current json output:see name value repeating.

[     {         "name": "raymala",         "folder": "787",         "paths": ["1.jpg", "2.jpg", "3.jpg"]     },     {         "name": "raymala",         "folder": "788",         "paths": ["1.jpg", "2.jpg", "3.jpg"]     },     {         "name": "falitiko",         "folder": "333",         "paths": ["1.jpg", "2.jpg", "3.jpg"]     } ] 

wanted json output:

[     {         "name":"raymala",         "random": [             {                 "folder": "787",                 "paths": ["1.jpg", "2.jpg", "3.jpg"]             },             {                 "folder": "788",                 "paths": ["1.jpg", "2.jpg", "3.jpg"]             }         ]     },     {         "name":"falitiko",         "random": [             {                 "folder": "332",                 "paths": ["1.jpg", "2.jpg", "3.jpg"]             },             {                 "folder": "333",                 "paths": ["1.jpg", "2.jpg", "3.jpg"]             }         ]     } ] 

$narray = array(); while($r = mysqli_fetch_assoc($result)) {       $narray[$r["name"]][] = array('folder' => $r['folder'], 'path' => explode(",", $r['path'])); } $output_arr = array(); foreach($narray $key=>$value){    $output_arr[]=array("name"=>$key,"random"=>$value); } echo json_encode($output_arr); 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -