2

Hi I want to put json nested object with this format:

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

into this json array :

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

and will result in this format :

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

my current code :

$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;

How I can do that? Thanks

y45
  • 47
  • 3
  • 9

1 Answers1

2

Check this:

$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;

Code in action: eval.in

Sumoanand
  • 8,835
  • 2
  • 47
  • 46
  • May I ask you a questions, when I call `alert(data)` after success response in ajax, why the result represented with [object][object] sometime `undefined`? this is a correct output? thanks – y45 May 28 '13 at 20:12
  • check this post to print objects: `http://stackoverflow.com/questions/1625208/print-content-of-javascript-object` – Sumoanand May 28 '13 at 20:15