我将两个数组组合,然后编码为JSON输出。我在第二个数组的开头在JSON输出中获得了一个时髦的值。这是"0":
项。我无法弄清楚这是从哪里来的。
组合数组并输出json:
$combine = array_merge(array('array1'=>$bay_events, array('array2'=>$key_events)));
$finish = json_encode($combine);
$callback = $_GET['callback'];
echo $callback.$finish;
JSON输出:
{
"array1": [
{
"event_id": "3914",
"event_name": "Test"
}
],
"0": { <--- this should not be here
"array2": [
{
"event_id": "3913",
"event_name": "Testssdgs This Is how"
}
]
}
}
我无法弄清楚"0":
的来源。帮助
答案 0 :(得分:3)
正是因为这条线
$combine = array_merge(array('array1'=>$bay_events, array('array2'=>$key_events)));
试试这个
$combine = array_merge(array('array1'=>$bay_events, 'array2'=>$key_events));
根据您的数据进行测试,此处为
$bay_events = array('event_id' => '3914', 'event_name' => 'Test');
$key_events = array('event_id' => '3913', 'event_name' => 'Testssdgs This Is how');
$combine = array_merge(array('array1'=>$bay_events, 'array2'=>$key_events));
echo json_encode($combine);die;
{
"array1":{"event_id":"3914","event_name":"Test"},
"array2":{"event_id":"3913","event_name":"Testssdgs This Is how"}
}
答案 1 :(得分:3)
您没有以正确的方式使用array_merge
问题
0
解释
$combine = array_merge(array('array1'=>$bay_events, array('array2'=>$key_events)))
^ ^ ^
| | |
Only One array ----+ Fist Index Second Index Another array
这是使用array_merge
$combine = array_merge(array('array1'=>$bay_events), array('array2'=>$key_events));
或者完全删除
$combine = array('array1'=>$bay_events, 'array2'=>$key_events);