1

YES, i read other posts but i still can ́t figure this out...

I have a multi-dimensional array in php.

json_encode($myarray);

Gives me something like this...

{"1":[0,0.46,0.23],"2":[0,0.71,0.33],"3":[0,0.7,0.54]}

Yes, maaaany floaties. I love them ;). How can i parse it with jQuery? I tried

var myarray = $.parseJSON(<?php echo json_encode($myarray); ?>);
alert(myarray[0][0].val());

but it doesn ́t work :/. Maybe i ́m just to dumb right now.

Thanks for any suggestions!

asked Sep 12, 2011 at 21:09
2
  • Why doesnt this work? Perhaps, in your sample, it is because you dont enclose your result in string tags. You should debug your code, view the source or check out the error log. Commented Sep 12, 2011 at 21:14
  • parseJSON() is for strings that contain a JSON value. JSON itself however is legal JavaScript, no need to wrap it in a string in this case. Commented Sep 12, 2011 at 21:14

2 Answers 2

4
var myArray = <?php echo json_encode($myarray); ?>

Since JSON is Javascript, there is no real parsing from one data type to another. JSON is a subset of javascript, its just compatible. Especially jQuery doesnt have anything to do with this.

If the data comes from an unknown source (e.g. the user) you'd want to validate it first.

answered Sep 12, 2011 at 21:10
Sign up to request clarification or add additional context in comments.

Comments

1

why would you parse it?

var json = <?php echo json_encode($array); ?>
answered Sep 12, 2011 at 21:11

3 Comments

json_decode accepts the JSON string, and spits out the PHP object. You'd want to use encode, which does it the other way around.
@TJHeuvel: sure, it was just a type :)
Thank you, perfect! Now i even understand the concept! :D

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.