As you know PHP is a Server-side Scripting language. It executes only at the time of page loading and reaches response to the browser as HTML. So PHP multidimensional array cannot assign to JavaScript array directly, since JavaScript is a Client-side Scripting language. That means it runs inside the browser.
So when you need to use multi-dimensional array (from database or elsewhere) as JavaScript array, a better way to do this is the use of JSON array, which is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
First convert the PHP array to JSON array using json_encode function in PHP.
For example,
$json_array=json_encode($data); // $ data is a multidimensional PHP array.
Now $json_array is JSON Array. It can be used as JavaScript array in PHP after parsing using jQuery.
Don’t forget to include jQuery to your PHP page to use jQuery.parseJSON() function. (If you are not using any jQuery, download from jquery.com)
// In JavaScript
var jsonObj = jQuery.parseJSON('<?php echo $json_array; ?>');
Now jsonObj is a JavaScript multidimensional array.
Filed under: PHP Tagged: javascript, javascript Array, jQuery, JSON, multi dimensional array, php, php array, Programming Image may be NSFW.
Clik here to view.

Clik here to view.
