Is It Possible To Return Json_encode() But As An Array And Not An Object, So I Can Use `.filter()` Or `.foreach()` Methods Easily
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 thi
Solution 1:
You need to change the main object to an Array. I think below code is what you're looking for.
Your Input :
vardata = {
"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"}
};
Convert Object to Array
var result = Object.keys(data).map(function(key) {
return data[key];
});
Now you can use Filter for the converted array
var filtered = result.filter(row => { return row.user_id > 1; });
And Filtered Result is:
[{"user_id":2,"test":"","user_name":"potato1","isok":" true"},{"user_id":3,"test":"","user_name":"potato2","isok":" true"},{"user_id":4,"test":"","user_name":"potato3","isok":"locationd"}]
Hope this is what you're looking for and here is working demo link : https://playcode.io/282703?tabs=console&script.js&output
Post a Comment for "Is It Possible To Return Json_encode() But As An Array And Not An Object, So I Can Use `.filter()` Or `.foreach()` Methods Easily"