0

I have php array like this

[0]->imgae_name=1
 image_url=a
[1]->imgae_name=2
 image_url=b

i want to convert this array in javascript array how can i do this?

asked May 21, 2013 at 9:27

3 Answers 3

1
json_encode($your_array);

is the right way to do this. It converts your php array to this string:

[{imgae_name:1,image_url:"a"},{imgae_name:2,image_url:"b"}]

Then you only have to assign it to a variable in a script tag.

answered May 21, 2013 at 9:28
Sign up to request clarification or add additional context in comments.

Comments

0

You could convert the PHP array to JSON using json_encode, then echo the json string into the Javascript in your page and use Javascript JSONObject to convert it into a JS array

answered May 21, 2013 at 9:29

3 Comments

I think eval is more then enough in javascript :D
@JeffLee not to split hairs, but doesn't eval() return an Object not an Array? I know the difference is little, but just going with the OP's question here. ;)
I see :D hahahaa I like json more the array :D
0

In php

<?
$array = array("A" => "1", "B" => "2");
echo json_encode($array);
?>

In Javascript

var mObject = eval("(" + phpecho + ")");

Then you can access the value :

 mObject.A // 1
answered May 21, 2013 at 9:31

Comments

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.