2

嗨,我想用这种格式放置 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;

我怎么能这样做?谢谢

4

1 回答 1

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

于 2013-05-28T16:16:05.653 回答