我在php中合并了两个数组。这是我的代码
$message = array_merge($message2, $message1);
echo json_encode($message);
这里我得到数据,但这里打印“0”和“1”
{"username":"ji5vajng","gender":"MALE","profilepic":"http:\/\/li336-153.members.linode.com\/services\/uploads\/users\/thumbs\/000000000000000Sunset.jpg","status":"1","nickname":"laddu","0":{"username":"i3vnbtcz","gender":"MALE","profilepic":"http:\/\/li336-153.members.linode.com\/services\/uploads\/users\/thumbs\/7BB46853-D79D-5E17-807F-FD0666AF21BBi3vnbtcz-2011-06-13 11:04:26 +0000.png","status":"1","nickname":"Cool"},"1":{"username":"oq5mjbvb","gender":"male","profilepic":"http:\/\/li336-153.members.linode.com\/services\/uploads\/users\/thumbs\/7BB46853-D79D-5E17-807F-FD0666AF21BBoq5mjbvb-2011-06-13 05:45:57 +0000.png","status":"1","nickname":""},"2":{"username":"ejzxm2oz","gender":"MALE","profilepic":"http:\/\/li336-153.members.linode.com\/services\/uploads\/users\/thumbs\/A0000015BAB1CAreceived_0.jpg","status":"1","nickname":"laddu"}}
答案 0 :(得分:1)
您所描述的是json_encode
Docs的正常行为,如果数组的字符串键不是数字,则数字键也将被编码。
$message1 = array('foo');
$message2 = array('key' => 'bar');
$message = array_merge($message2, $message1);
echo json_encode($message); # {"key":"bar","0":"foo"}
正如你所看到的那样是json中的对象表示。
如果所有键都是数字键,则它们不会成为编码的一部分:
$message1 = array('foo');
$message2 = array('bar');
$message = array_merge($message2, $message1);
echo json_encode($message); # ["bar","foo"]
这是json中的数组表示。
答案 1 :(得分:0)
我不认为这个结果有问题。因为合并的数组应该是......
(array(2)) '0' => array(username => 'foo',/* and so on */),
'1' => array(username => 'bar',/* and so on */)
答案 2 :(得分:-1)
难道你不能只是预先获取数组的值然后在其上放一个json_encode吗?
foreach ($message as $value) {
echo json_encode($value);
}