JSON - 如何在php中将数组中的嵌套对象合并

时间:2013-05-28 16:07:17

标签: php json

您好我想以这种格式放置json嵌套对象:

[{"lat":-6.92015,"lon":107.67024,"value":0.1},{"lat":-6.88283,"lon":107.60149,"value":0.1},..]

进入这个json数组:

{"max": 30, "data": [...]}

并将生成以下格式:

{"max": 30, "data": [{"lat":-6.92015,"lon":107.67024,"value":0.1},...]}

我目前的代码:

$data[] = array(lat=>(float)$row["geo_lat"], lon=>(float)$row["geo_long"], value=>1);
$post_data = json_encode(array('max' => 30, 'data' => $data));
echo $post_data;

我怎么能这样做?感谢

1 个答案:

答案 0 :(得分:2)

检查一下:

$array = json_decode('[{"lat":-6.92015,"lon":107.67024,"value":0.1},{"lat":-6.88283,"lon":107.60149,"value":0.1}]');

print_r($array);

$final_array = array('max'=>30,'data'=>$array);
print_r($final_array);

$output = json_encode($final_array);
print $output;

行动准则:eval.in