php - Make Array of Objects from json_encode -
i have object
stdclass object ( [reportdate] => 2014-02-02 [shops] => array ( [24] => stdclass object ( [shopid] => 24 [cashiers] => array ( [1] => stdclass object ( [cashierid] => 1 [products] => array ( [moneyin] => 46 [moneyout] => 215.14 ) ) ) ) ) )
and when make json_encode on json string
{ "reportdate":"2014-02-02", "shops":{ "24":{ "shopid":24, "cashiers":{ "1":{ "cashierid":1, "products":{ "moneyin":"46", "moneyout":"215.14" } } } } } }
this result not wanted. want array of objects.
so instead of "shops":{
want "shops":[
instead of "cashiers":{
want "cashiers":[
, on.
where ever there array in stdclass want array , there stdclass want object.
so doing wrong in structuring initial stdclass object.
an associative array results in object, you've seen. produce json array need array composed of arrays.
here's example
$shops = [['shopid'=>24, 'cashiers'=>[['cashierid'=>1]]]];
produces
[ { "shopid":24, "cashiers":[{"cashierid":1}] } ]
and here's live runnable demo
Comments
Post a Comment