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?

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.

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.