Jump to content

copy array from php to javascript


hukadeeze

Recommended Posts

I'm trying to copy the contents of a numerically indexed php array ($menu) to a javascript array (javascriptmenu). I'm not too familiar with js. The only idea I had was to create an array in js and then set it equal to the php array:

 

var javascriptmenu = new Array()
var javascriptmenu = '.$menu.'

 

I'm using the '..' because this block is already contained within single quotes. When I try to alert an item in the array javascriptmenu:

 

alert(javascriptmenu[0])

 

I get this message:

 

undefined

 

Am I doing something wrong, or is this just not possible?

 

Link to comment
Share on other sites

It is but you have to be a bit more precise in the definition...

 

<?php
$menu = array('apple', 'banana', 'peach');
echo "<script>\n";
echo "var jsmenu = new Array();\n";
foreach($menu as $item){
    echo "jsmenu.push($item)\n";
}
echo "</script>";
?>

 

Should work...

Link to comment
Share on other sites

php can echo out javascript as text so just loop thru the php array and write out javascript as text that will create and assign the js array browser side:

<script type="text/javascript">
var javascriptmenu = new Array();
<?php 
    for ($i=0; $i < count($menu); $i++) { 
        echo "javascriptmenu[$i] = '" . $menu[$i] . "';"; //outputting javascript!
    } 
?>
</script>

Link to comment
Share on other sites

I ran into a problem. This block of html and js is being returned from a function so it's all inside single quotes (Everything but the loop is just there to show the structure).

 

return '

<html>
<script>

'foreach($menu as $item){
echo "jsmenu.push($item)\n";
}'

</script>
</html>

';

 

When I try to close with ' run the loop and then open again with ' I get an error that says unexpected foreach.

Link to comment
Share on other sites

Push is used to "push" an element onto the end of an array.

 

I would break up the return info.

 

<?php
....
$text = "";
foreach($menu as $item){
    $text .= "jsmenu.push($item)\n";
}
return $text; //$text will have all the js push items to be echo'd out later
?>

 

Adding whatever tags you need before or after the foreach results in $text. That would work better (and is more readable) than your previous return.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.