Having problems with getting arrays into arrays for json-encode
heres the function: http://pastebin.com/tQLjzzbH
I want it to return this:
{"years":[
{
"yName":"2011",
"yAlbums:[
{
auID:"1234",
aID:"456",
etc..
},
{
auID:"12345",
aID:"4567",
etc..
}
},
{
"yName":"2010",
"yAlbums:[
{
auID:"2234",
aID:"556",
etc..
}
}
]}
but its only returning this:
{"years":[
{"yName":"2011"},
{"yName":"2010"}]}
Been trying to get this for ages, am totally lost now. Some help wo开发者_高级运维uld be appreciated.
Thanks.
You have read your result to the very end by
while($data2 = mysql_fetch_assoc($result))
{
$yearsArrayRaw[] = $data2['album_year'];
}
Add mysql_data_seek($result,0)
before inner while
, to line 36. Also, I would suggest rewriting this function to have single loop, using something like
$year_albums= array();
while ($data = mysql_fetch_assoc($result)){
if (empty($years[$data['album_year']])){
$year_albums[$data['album_year'] = array(
'yName'=>$data['album_year'],
'yAlbums'=>array()
);
}
//...album creation logic here...//
$year_albums[$data['album_year']['yAlbums'][] = $album;
}
//Converting arrays into objects
$years = array();
foreach ($year_albums as $year){
$years[] = (object)$year;
}
json_encode($years);
精彩评论