javascript - PHP encoding nested array as object -


i'm trying list of year , month php web site page. website works javascript, need data json.

there no problem generate information need, use json_encode on php, nested arrays turn object , because of this, can't have proper behavior on javascript.

this i'm doing generate array on php:

$list = sql("select distinct year(created) year order");  foreach ($list &$row) {     $row['month'] = array();      ($i=1; $i<=12; $i++) {         $row['month'][$i] = new stdclass();         $row['month'][$i]->month = date("m", strtotime(date("y")."-".$i."-01"));          //more information goes here...     } } 

if print $list using return print_r($list); result:

array (     [0] => array (         [year] => 2016         [month] => array (             [1] => stdclass object (                 [month] => jan             )             [2] => stdclass object (                 [month] => feb             )             //etc... 

but when use

return print_r(json_encode($list)); //or return print_r(json_encode($list, true)); 

all nested array turn object. example, month array, turn object, print screen of console.log of result:

enter image description here

is there way fix this? or doing wrong?

if need month names, replace these rows:

$row['month'][$i] = new stdclass(); $row['month'][$i]->month = date("m", strtotime(date("y")."-".$i."-01")); 

with this:

$row['month'][$i] = date("m", strtotime(date("y")."-".$i."-01")); 

you don't need std object tring accomplish.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -