简体   繁体   English

如何使用 json_encode() 将 PHP 数组创建为 JSON 数组;

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

I've encoded an Array I've made using the inbuilt json_encode();我已经使用内置的 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.您希望它看起来像的示例不是有效的 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.您在data下有多个项目,因此它们需要在一个数组中。 Think of JSON like it's a normal variable you have.将 JSON 视为您拥有的普通变量。 You can't store a list of items like that without it being an array.如果没有数组,您就无法存储这样的项目列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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