Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

javascript - 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 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to change the main object to an Array. I think below code is what you're looking for.

Your Input :

 var data = {
    "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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...