通过多维数组生成JSON

时间:2017-06-23 19:57:21

标签: php arrays json

我需要通过在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"
        }
    ]
}

我在这里做错了什么?

0 个答案:

没有答案