JAVASCRIPT - HOW TO Copy PHP Array to Javascript Array - Javascript Source Code

JAVASCRIPT - HOW TO Convert/Copy/Save PHP Array to a Javascript Array with ONE Short Line of Javascript Code.

Home Short:

A PHP array can't be used by Javascript as is, but with a simple one short line of Javascript code you can easy copy and pass a PHP array to a Javascript array.

This is ALMOST the same code as getting the value of a PHP variable in Javascript with the difference of json encoding the PHP array before you can access and use that array in Javscript.

Try copying a php array to Javscript for yourself. Although you can't see it when you view the source code of this page because php executes on the web server and not in the web browser, there is php code as follows: <?php $arrPhp = array("apples", "oranges", "peaches", "pears");?>.

This php code creates a php array with the following 4 items added to the php array:

  1. apples.
  2. oranges.
  3. peaches.
  4. pears.
S
H
A
R
E