Converting php array to javascript array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
code_worm
Forum Newbie
Posts: 19
Joined: Thu Dec 10, 2009 3:00 pm

Converting php array to javascript array

Post by code_worm »

Hi, I want to create a javascript array that duplicates the values within a PHP array.
I have looked at many resources online, but due to little understanding i havent implemented them as of yet.

What i am asked to do is duplicate this php array to a javascript array within the head of my php page. Does this mean that it needs to be a function?
Most of the code i see is relevant to sql stuff, I would be very greatfull for your help or even relevant resources

Thanks cw
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Converting php array to javascript array

Post by AbraCadaver »

json_encode()
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
code_worm
Forum Newbie
Posts: 19
Joined: Thu Dec 10, 2009 3:00 pm

Re: Converting php array to javascript array

Post by code_worm »

Json_encode()
Just read up on it, it says that it returns a string on execution.
I want to return a populated array, do you mean to say populate the javascript array with json_encode() ?
My experiance in both javascript and php are limited sorry
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Converting php array to javascript array

Post by AbraCadaver »

code_worm wrote:Json_encode()
Just read up on it, it says that it returns a string on execution.
I want to return a populated array, do you mean to say populate the javascript array with json_encode() ?
My experiance in both javascript and php are limited sorry
Yes (example):

Code: Select all

echo '<script type="text/javascript">'
'var myJSarray = ' . json_encode($myPHParray) . ';'
'</script>';
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
code_worm
Forum Newbie
Posts: 19
Joined: Thu Dec 10, 2009 3:00 pm

Re: Converting php array to javascript array

Post by code_worm »

Thanks, I have never implemented this before.
Am i stupid in asking this, I am required to do this in the head section...
As this code goes in the head will it still work if the php array declaration is in the body. Does this mean i need to create a function?
code_worm
Forum Newbie
Posts: 19
Joined: Thu Dec 10, 2009 3:00 pm

Re: Converting php array to javascript array

Post by code_worm »

What i am creating is a dynamic html page
If i create the javascript array within the head of my php page will i be able to acess this array through my js functions?
Post Reply