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

2
  • What's the problem with what you have? Commented May 28, 2013 at 16:11
  • No problems man, sorry for that, I've syntax error on that line, thanks for your time. Commented May 28, 2013 at 16:23

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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
check this post to print objects: http://stackoverflow.com/questions/1625208/print-content-of-javascript-object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.