简体   繁体   中英

How To Create PHP Array to JSON Array using json_encode();

I've encoded an Array I've made using the inbuilt json_encode(); function. I need it in the format of an Array of Arrays like so:

{
  "status": "success",
  "data": [
    {
      "Info": "A",
      "hasil": "AA"
    },
    {
      "Info": "B",
      "hasil": "BB"
    },
    {
      "Info": "C",
      "hasil": "CC"
    },
    {
      "Info": "D",
      "hasil": "DD"
    },
    {
      "Info": "F",
      "hasil": "FF"
    },
    {
      "Info": "G",
      "hasil": "GG"
    },
    {
      "Info": "H",
      "hasil": "HH"
    },
    {
      "Info": "I",
      "hasil": "II"
    },
    {
      "Info": "J",
      "hasil": "JJ"
    }
  ]
}

I need it is returning as:

{
  "status": "success",
  "data": 
    {
      "Info": "A",
      "hasil": "AA"
    },
    {
      "Info": "B",
      "hasil": "BB"
    },
    {
      "Info": "C",
      "hasil": "CC"
    },
    {
      "Info": "D",
      "hasil": "DD"
    },
    {
      "Info": "F",
      "hasil": "FF"
    },
    {
      "Info": "G",
      "hasil": "GG"
    },
    {
      "Info": "H",
      "hasil": "HH"
    },
    {
      "Info": "I",
      "hasil": "II"
    },
    {
      "Info": "J",
      "hasil": "JJ"
    }

}

Your example of what you want it to look like is not valid JSON. It needs to be outputted like how you see it currently being outputted. You have multiple items under data and as such they need to be in an array. Think of JSON like it's a normal variable you have. You can't store a list of items like that without it being an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM