Sserpyc
Sserpyc

Reputation: 15

Passing a multi-dimensional array from PHP to jQuery

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!

Upvotes: 1

Views: 1034

Answers (2)

genesis
genesis

Reputation: 50976

why would you parse it?

var json = <?php echo json_encode($array); ?>

Upvotes: 1

TJHeuvel
TJHeuvel

Reputation: 12608

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.

Upvotes: 4

Related Questions