I need to generate from an array this json:
我需要从一个数组生成这个json:
{
“authentication”: {
“username”: “test”,
“password”: “test”
},
“msg”: [
{
“name”: “jDOE”,
“msg”: “Hello”,
“recipients”: [
{
“gm”: “385951111111”
},
{
“gm”: “385952222222”
},
{
“gm”: “385953333333”
}
]
}
]
}
This is easy creating just the array but, if you see the GM key repeats 3 times. In PHP I think we can't have duplicate keys in associative arrays. So how can I replicate this....maybe an object? a string?....after generate the structure I use the funciton json_encode to generate the json.
这很容易创建数组但是,如果你看到GM键重复3次。在PHP中,我认为我们不能在关联数组中有重复的键。那我怎么能复制这个......也许是一个对象?一个字符串?....生成结构后我使用函数json_encode生成json。
This is the array I'm using to generate the json:
这是我用来生成json的数组:
$data = array(
'authentication' => array(
'username' => 'BisA4Corp1',
'password' => 'Xls2smst5',
),
'messages' => array(
'name' => 'jDOE',
'msg' => 'Mensaje de prueba',
'recipients' => array('gm' => '3387967849'),
),
);
Thanks!
2 个解决方案
#1
recipients
must be an array itself, so your array should be like this:
收件人必须是一个数组本身,所以你的数组应该像这样:
$data = array(
'authentication' => array(
'username' => 'BisA4Corp1',
'password' => 'Xls2smst5',
),
'messages' => array(
'name' => 'jDOE',
'msg' => 'Mensaje de prueba',
'recipients' => array(
array('gm' => '3387967849'),
array('gm' => '3387967849'),
array('gm' => '3387967849'),
),
),
);
#2
Use multidimential array as php doesnt allow your code.
使用multidimential数组,因为php不允许你的代码。
<?php
$data = array(
'authentication' => array(
'username' => 'BisA4Corp1',
'password' => 'Xls2smst5',
),
'messages' => array(
'name' => 'jDOE',
'msg' => 'Mensaje de prueba',
'recipients' => array( array('gm' => '3387967849'), array('gm' => '385952222222'))
)
);
echo "<pre>";
print_r($data);
Output
Array
(
[authentication] => Array
(
[username] => BisA4Corp1
[password] => Xls2smst5
)
[messages] => Array
(
[name] => jDOE
[msg] => Mensaje de prueba
[recipients] => Array
(
[0] => Array
(
[gm] => 3387967849
)
[1] => Array
(
[gm] => 385952222222
)
)
)
)
#1
recipients
must be an array itself, so your array should be like this:
收件人必须是一个数组本身,所以你的数组应该像这样:
$data = array(
'authentication' => array(
'username' => 'BisA4Corp1',
'password' => 'Xls2smst5',
),
'messages' => array(
'name' => 'jDOE',
'msg' => 'Mensaje de prueba',
'recipients' => array(
array('gm' => '3387967849'),
array('gm' => '3387967849'),
array('gm' => '3387967849'),
),
),
);
#2
Use multidimential array as php doesnt allow your code.
使用multidimential数组,因为php不允许你的代码。
<?php
$data = array(
'authentication' => array(
'username' => 'BisA4Corp1',
'password' => 'Xls2smst5',
),
'messages' => array(
'name' => 'jDOE',
'msg' => 'Mensaje de prueba',
'recipients' => array( array('gm' => '3387967849'), array('gm' => '385952222222'))
)
);
echo "<pre>";
print_r($data);
Output
Array
(
[authentication] => Array
(
[username] => BisA4Corp1
[password] => Xls2smst5
)
[messages] => Array
(
[name] => jDOE
[msg] => Mensaje de prueba
[recipients] => Array
(
[0] => Array
(
[gm] => 3387967849
)
[1] => Array
(
[gm] => 385952222222
)
)
)
)