You do not have to call parseJSON since the output of json_decode is a javascript literal. Just assign it to a variable.

var tempArray = <?php echo json_encode($php_array); ?>;

You should be able then to access the properties as

alert(tempArray[0].Key);
Answer from Orangepill on Stack Overflow
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-convert-Multidimensional-PHP-array-to-JavaScript-array
How to convert Multidimensional PHP array to JavaScript array?
July 30, 2019 - It works for a single as well as the multidimensional array. Use the json_encode() method to achieve this. The following is the Multi-dimensional PHP array. $myArr= array( array('Amit', 'amit@example.com'), array('Rahul, 'rahul@example.com'), ); Converting PHP multidimensional array into JavaScript.
๐ŸŒ
CodexWorld
codexworld.com โ€บ home โ€บ how to guides โ€บ how to convert php array to javascript array
How to Convert PHP Array to JavaScript Array - CodexWorld
February 26, 2025 - Convert PHP Array to JavaScript Array - Use json_encode() function to convert PHP indexed, associative, and multidimensional array to JavaScript array. The example code to convert PHP array and access in JavaScript.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 54261957 โ€บ parsing-php-multidimensional-array-to-javascript-multidimensional-array
parsing php multidimensional array to javascript multidimensional array - Stack Overflow
Voila, my problem: my PHP variable is a multidimensional array var_dump($php_data); result: array(3) { [0]=> array(5) { ["grey"]=> string(3) "aaa" ["black"]=> string(3) "bbb" ["blue"]=...
Find elsewhere
๐ŸŒ
Position Is Everything
positioniseverything.net โ€บ home โ€บ php object to array: a comprehensive guide with examples
PHP Object to Array Conversion: A Complete and Easy-to-Follow Guide
December 29, 2025 - In this example of converting an associative array into an object, the array is converted to object using, $object = (object) $array. Lastly, we print out the output using the function var_dump(variable of an object is written here).
๐ŸŒ
sebhastian
sebhastian.com โ€บ php-array-to-javascript-array
Convert PHP array to JavaScript array (with code examples) | sebhastian
September 19, 2022 - Since a JavaScript object is similar to an associative array, so you can access the value using the key as well. ... <?php // ๐Ÿ‘‡ create a PHP associative array $my_array = [ "name" => "nathan", "age" => 28, ]; ?> <body> <!-- ๐Ÿ‘‡ Add a script tag for JavaScript --> <script> const arr = <?php echo json_encode($my_array); ?>; // ๐Ÿ‘‡ the result will not be an array according to JavaScript console.log(Array.isArray(arr)); // ๐Ÿ‘‡ access the object properties like associative array console.log(arr["name"]); console.log(arr["age"]); </script> </body>
๐ŸŒ
Fellow Tuts
fellowtuts.com โ€บ home โ€บ php โ€บ 3 ways โ€“ multidimensional array to object conversion
3 Ways - Multidimensional Array to Object Conversion
December 18, 2018 - But if any only if the array is not multidimensional. In other words, itโ€™s not a nested array (array in the array). If you did so then only the top level keys => values of the array will be converted to object. A combination of PHPโ€™s JSON encode and decode functions can magically do the conversion.
๐ŸŒ
Medium
medium.com โ€บ laravelnepal โ€บ convert-your-deeply-nested-objects-to-array-50b3286bc323
Convert your deeply nested objects to array | by Suman Shrestha | laravelnepal | Medium
October 25, 2017 - Convert your deeply nested objects to array In php, you can convert nested object to array in many ways. You can loop through the properties. You can (array) $object to change. Or create an entirely โ€ฆ
Top answer
1 of 2
126

A quick way to do this is:

$obj = json_decode(json_encode($array));

Explanation

json_encode($array) will convert the entire multi-dimensional array to a JSON string. (php.net/json_encode)

json_decode($string) will convert the JSON string to a stdClass object. If you pass in TRUE as a second argument to json_decode, you'll get an associative array back. (php.net/json_decode)

I don't think the performance here vs recursively going through the array and converting everything is very noticeable, although I'd like to see some benchmarks of this. It works, and it's not going to go away.

2 of 2
7

The best way would be to manage your data structure as an object from the start if you have the ability:

$a = (object) array( ... ); $a->prop = $value; //and so on

But the quickest way would be the approach supplied by @CharlieS, using json_decode(json_encode($a)).

You could also run the array through a recursive function to accomplish the same. I have not benchmarked this against the json approach but:

function convert_array_to_obj_recursive($a) {
    if (is_array($a) ) {
        foreach($a as $k => $v) {
            if (is_integer($k)) {
                // only need this if you want to keep the array indexes separate
                // from the object notation: eg. $o->{1}
                $a['index'][$k] = convert_array_to_obj_recursive($v);
            }
            else {
                $a[$k] = convert_array_to_obj_recursive($v);
            }
        }

        return (object) $a;
    }

    // else maintain the type of $a
    return $a; 
}

Hope that helps.

EDIT: json_encode + json_decode will create an object as desired. But, if the array was numerical or mixed indexes (eg. array('a', 'b', 'foo'=>'bar') ), you will not be able to reference the numerical indexes with object notation (eg. $o->1 or $o[1]). the above function places all the numerical indexes into the 'index' property, which is itself a numerical array. so, you would then be able to do $o->index[1]. This keeps the distinction of a converted array from a created object and leaves the option to merge objects that may have numerical properties.

๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 41564641 โ€บ multidimensional-array-javascript
php - Multidimensional Array Javascript - Stack Overflow
In blade view i do a foreach ($personArray as $pa) to saw for example all names (inside foreach i do -> $pa['Name'] and we print all names). For now it is ok. But i need to use this same array in javascript. I do this: var pArray= <?php echo json_encode(json_encode($personsArray)) ?>; and make an alert and he show the array (in this time the array are "converted" to javascript).
๐ŸŒ
IQCode
iqcode.com โ€บ code โ€บ php โ€บ php-convert-multidimensional-object-to-array
php convert multidimensional object to array Code Example
September 29, 2021 - multi array to single array php php array is multiple or single? how to convert objet to array in laravel how convert array bidimensional in arrray php multidimensional array to single array turn multi dimensional array into array php turn array into object laravel change object to array how to get a object out of multidimensional object/array in php change multidimensional object in array php multiduimedimensional array rio singel dimentional array php laravel object array convert into normal array using foreach multivalue array to single value php php convert dataset to associative array php