我试图通过json请求将值传递给api。
$payload = json_encode( array("phones"=> "971xxxxxxx",
"emails"=> "fadfad@xyz.com",
"id"=> "1"
) );
如何将以下多维值传递给json请求,如上面的代码?
{ "contactList": [ { "phones" : ["+91 9000000034"], "emails" : [fadfad@xyz.com], "id" : 1 }, { "phones" : ["+91 903-310-00-001"], "emails" : [krs@xyz.in], "id" : 2 } ] }
答案 0 :(得分:0)
将数据存储为嵌套/多维数组,然后再将其传递给json_encode。
$array = array();
$array["contactList"] = array(
array( "phones"=> "971xxxxxxx",
"emails"=> "fadfad@xyz.com",
"id"=> "1"
),
array( "phones"=> "+91 903-310-00-001",
"emails"=> "krs@xyz.in",
"id"=> "2"
)
);
$payload = json_encode($array);
echo $payload;
产生
{"contactList":[{"phones":"971xxxxxxx","emails":"fadfad@xyz.com","id":"1"},{"phones":"+91 903-310-00-001","emails":"krs@xyz.in","id":"2"}]}
如果您需要保存数组中的电话号码等值,只需将它们包装在一个数组中即可。