How to get javascript array value in php

it was always a question for me that how can i use an array value in javascript while that array is defined in my php scripts

For example consider reading some values from a file and use it in javascript.

what's ur plan to do so ?

asked Feb 1, 2010 at 21:13

How to get javascript array value in php

You can use the json_encode function, to safely return a JSON object which you can use directly in JavaScript:




Outputs:


answered Feb 1, 2010 at 21:22

1

Something like this?

\n";
  echo "var js_array = new Array();\n";

  $ix = 0;
  foreach($php_array as $key => $value) {
     echo "js_array[$key] = $value;\n";
  }

  # Rest of JavaScript.....
  echo "\n";
?>

And perhaps for more info:
http://www.scratch99.com/2008/03/creating-javascript-array-dynamically-from-php-array/

answered Feb 1, 2010 at 21:19

How to get javascript array value in php

YvoYvo

18.2k11 gold badges69 silver badges89 bronze badges

2

JSON is your choice, since some PHP 5.x version, PHP contains a function json_encode().


As usual, some nice guys wrote json_encode() functions for older PHP version, checkout the comments on php.net.

answered Feb 1, 2010 at 21:22

FrunsiFrunsi

7,0395 gold badges35 silver badges42 bronze badges

0

The array in your PHP needs to be exposed to JavaScript in some way. If you want the array available to JS on the initial page load, you might do it like this:


If the variable doesn't need to be created on the initial page load, you can do something similar, but use an AJAX call. Simply have your PHP script respond with the array formatted for JS, and store the result of the call in your JS variable.

answered Feb 1, 2010 at 21:23

JimmyJimmy

34.1k13 gold badges78 silver badges96 bronze badges

Variant on the PHP to JS without json extension, using join / implode and no loop construct:



answered Feb 1, 2010 at 21:35

bdlbdl

1,45412 silver badges15 bronze badges

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Passing PHP Arrays to JavaScript is very easy by using JavaScript Object Notation(JSON).

    Method 1: Using json_encode() function: The json_encode() function is used to return the JSON representation of a value or array. The function can take both single dimensional and multidimensional arrays.

    Steps:

    • Creating an array in PHP:
       "Geeks", 
              1 => "for", 
              2 => "Geeks", 
          );
      ?>
      
    • Using json_encode() function to retrieve the array elements
      var passedArray = ;

    Example:

    php

    // Create an array

    $sampleArray = array(

        0 => "Geeks", 

        1 => "for", 

        2 => "Geeks", 

    )

    ?>

    <script>

    // Access the array elements

    var passedArray = 

        php echo json_encode($sampleArray); ?>;

    // Display the array elements

    for(var i = 0; i < passedArray.length; i++){

        document.write(passedArray[i]);

    }

    Output:

    GeeksforGeeks

    Method 2: Using PHP implode() function: The implode() is used to join the elements of an array. The implode() function is the alias of join() function and works exactly same as that of join() function.
    The implode() function is used to build a string that becomes an array literal in JavaScript. So, if we have an array in PHP, we can pass it to JavaScript as follows:

    var passedArray = ;
    

    Example:

    php

    // Creating a PHP Array

    $sampleArray = array('Car', 'Bike', 'Boat');

    ?>

    <script type="text/javascript">

    // Using PHP implode() function

    var passedArray = 

        php echo '["' . implode('", "', $sampleArray) . '"]' ?>;

    // Printing the passed array elements

    document.write(passedArray);

    script>

    Output:

    Car, Bike, Boat

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    How to get PHP array value in JavaScript?

    Passing PHP Arrays to JavaScript is very easy by using JavaScript Object Notation(JSON). Method 1: Using json_encode() function: The json_encode() function is used to return the JSON representation of a value or array. The function can take both single dimensional and multidimensional arrays.

    Can you use PHP array in JavaScript?

    You can easily use PHP array in javascript you only need to convert PHP array into JSON format Using json_encode() function. PHP array can be converted to JavScript array and accessible in JavaScript. Whatever the array type is, a single or multidimensional or indexed or associative array.

    How to assign PHP array to JavaScript?

    PHP array can be used in JavaScript, whatever the array is a single or multidimensional or indexed or associative array. You can convert PHP array to JavaScript array easily with a single line of code. Using json_encode() function, PHP array can be converted to JavScript array and accessible in JavaScript.

    How do I convert a string to an array in JavaScript?

    The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.