Php 通过多维数组生成JSON

Php 通过多维数组生成JSON,php,arrays,json,Php,Arrays,Json,我需要通过在PHP中将其全部整合在一起来重新创建: { "content": [ { "type": "text/plain", "value": " " } ], "from": { "email": "dx@sendgrid.com", "name": "Ryan Smith" }, "personalizations": [ { "to": [ { "ema

我需要通过在PHP中将其全部整合在一起来重新创建:

 {
  "content": [
    {
      "type": "text/plain",
      "value": " "
    }
  ],
  "from": {
    "email": "dx@sendgrid.com",
    "name": "Ryan Smith"
  },
  "personalizations": [
    {
      "to": [
        {
          "email": "elmer@sendgrid.com"
        }
      ]
    }
  ],
  "subject": "my subject"
}
这就是我所拥有的,但并不完全正确:

$content ='some long string';
$to =array('email'=>'elmer@sendgrid.com');

$objectArray = array(
    'content'=> array ('type'=>'text/html', 'value'=>$content),
    'from'=>array('from'=>'dx@sendgrid.com','name'=>'Ryan Smith'),
    'subject'=>'my subject',
    'personalizations'=> array('to'=>$to)
    );
$postfields = json_encode($objectArray);
编辑

对不起,点击得太快了

下面是返回的错误:

{
    "errors": [
        {
            "message": "Invalid type. Expected: array, given: object.",
            "field": "content",
            "help": "http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.content"
        },
        {
            "message": "Invalid type. Expected: array, given: object.",
            "field": "personalizations",
            "help": "http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#-Personalizations-Errors"
        }
    ]
}

我在这里做错了什么?

有什么不对的地方吗?请问一个问题。您在php中构建的数组将生成一个json,如您的示例所示。我看到两个不同之处。尝试比较$postfields的回声,并将结果与您的示例进行比较。确保将示例和回显文本打印或粘贴到浏览器开发控制台中的js变量中。(第一件事diff:example有from->email,而php有from->from。第二件事:需要一个数组围绕“to”所在的数组。)@Terminus基本上,看起来我缺少了所有的方括号,但不确定如何到达。嗯,你说得对。还需要另一个内容阵列。只要做一个小小的测试页面,
。。。echo json_编码($postfields)
并不断调整,直到与示例匹配。有什么不太正确的地方吗?请提出一个问题。您在php中构建的数组将生成类似于示例中的json。我看到两个不同之处。尝试比较$postfields的回声,并将结果与您的示例进行比较。确保将示例和回显文本打印或粘贴到浏览器开发控制台中的js变量中。(第一件事diff:example有from->email,而php有from->from。第二件事:需要一个数组围绕“to”所在的数组。)@Terminus基本上,看起来我缺少了所有的方括号,但不确定如何到达。嗯,你说得对。还需要另一个内容阵列。只要做一个小小的测试页面,
。。。echo json_编码($postfields)
并继续调整,直到与示例匹配为止。