I just want to make sure there is no such thing... because I cannot find anything mentioning this:
Currently, when I use json_encode($array)
, I get a json object that look like this:
{
"1": {"user_id":1,"test":"","user_name":"potato0","isok":"true"},
"2":{"user_id":2,"test":"","user_name":"potato1","isok":" true"},
"3":{"user_id":3,"test":"","user_name":"potato2","isok":" true"},
"4":{"user_id":4,"test":"","user_name":"potato3","isok":"locationd"}
}
and I would like to run the .filter()
/.forEach()
methods.
but they wont run on objects ({...}
) and will run on arrays ([...]
).
Edit: It seems that it's not sure what I am getting so this is a real var_dump and json_encode() example:
var_dump($array);
array (size=2)
'status' => boolean true
'data' =>
array (size=3)
'fruits' =>
array (size=9)
'fruit_id' => int 246
'fruit_name' => string 'banana' (length=15)
'vegtables' =>
array (size=9)
'veg_id' => int 253
'fruit_name' => string 'potato' (length=20)
echo json_encode(['status' => true, 'data' => $fruits]);
{
"status":true,
"data":{
"fruits":{
"fruit_id":246,
"fruit_name":"banana"
},
"vegtables":{
"veg_id":253,
"veg_name":"potato"
}
}
}
the json returned is defined as an "Object" (checked using typeof
)
** I'm not willing to turn my object to an array on the js side, I aware about this "trick" and I prefer to fetch a json array from php as an array, please focus on my question **
See Question&Answers more detail:
os