You can use json_encode() to make array as an json object like,

var words = <?php echo json_encode($wordsArray) ?>;// don't use quotes
$.each(words, function(key, value) {
    console.log('stuff : ' + key + ", " + value);
});
Answer from Rohan Kumar on Stack Overflow
🌐
sebhastian
sebhastian.com › php-array-to-javascript-array
Convert PHP array to JavaScript array (with code examples) | sebhastian
September 19, 2022 - When you have an associative PHP array, calling json_encode() will transform the array into a JavaScript object.
Discussions

passing PHP objects to javascript - Stack Overflow
However, how do you get an object from php to javascript? ... You can convert the PHP object to an array, and then use JSON function to encode it. After that, decode it from JavaScript. More on stackoverflow.com
🌐 stackoverflow.com
Converting Javascript Object into PHP array - Stack Overflow
$phparray = str_replace(':', ',', ... // Now convert it into PHP array · But it is not working. Looks like i cannot put $phparray variable here array($phparray) But, If instead of putting $phparray variable in array($phparray), If i put the output of $phparray manually, then, it works like: array("1","2","2","2","3","2","4","2") ... That's not a "Javascript array", that's JSON. ... It seems that your form submits a json object to the server ... More on stackoverflow.com
🌐 stackoverflow.com
How to convert a JavaScript object to a PHP array and get the raw string? - Stack Overflow
In my application I need to convert a JavaScript Object to a raw PHP array returned as text string. I would do this calling an API. I don't need to manupulate the array. In my JavaScript applicatio... More on stackoverflow.com
🌐 stackoverflow.com
php - How can I convert a JavaScript object to an array - Stack Overflow
I have the following javascript object, in an array. I am using PHP's json_encode to get this value ... I have searched around a lot and couldn't find an answer. Any help would be really appreciated. Thanks! ... You should try to convert object and its fields to arrays and then serialize result. More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How can I convert an array back into an object in PHP?
You can easily convert an array back to an object in PHP by casting the array to an object using typecasting, like this: $object = (object) $array;. This method is particularly useful when you need to work with data in an object-oriented way, such as when manipulating data within classes and methods. For nested arrays, JSON encoding/decoding is also an option to achieve the reverse conversion.
🌐
upgrad.com
upgrad.com › home › blog › software development › how to convert object to array in php? techniques and examples
How to Convert Object to Array in PHP – Quick & Simple Methods
Can I convert an object to an array in PHP while maintaining its methods?
No, converting an object to an array using methods like typecasting or get_object_vars() will only extract the object’s properties, not its methods. If you need to keep track of the methods alongside the properties, you would have to manage them separately or use serialization, which includes both data and methods, although it can’t directly preserve methods in an array.
🌐
upgrad.com
upgrad.com › home › blog › software development › how to convert object to array in php? techniques and examples
How to Convert Object to Array in PHP – Quick & Simple Methods
Can I use JSON encoding/decoding for all objects when learning how to convert object to array in PHP?
While JSON encoding/decoding is versatile, it isn't suitable for all objects when learning how to convert object to array in PHP. For instance, it doesn’t work well with resources (like file handles or database connections) or objects with non-UTF8 characters. Always check the compatibility of your object before using JSON methods, especially when dealing with special data types or large resources.
🌐
upgrad.com
upgrad.com › home › blog › software development › how to convert object to array in php? techniques and examples
How to Convert Object to Array in PHP – Quick & Simple Methods
🌐
TutorialsPoint
tutorialspoint.com › How-to-convert-PHP-array-to-JavaScript-array
How to convert PHP array to JavaScript array?
June 18, 2020 - You can use PHP array in JavaScript. It works for the single as well as the multidimensional array. Use the json_encode() method to achieve this. ... Converting PHP array into JavaScript.
🌐
Upgrad
upgrad.com › home › blog › software development › how to convert object to array in php? techniques and examples
How to Convert Object to Array in PHP – Quick & Simple Methods
September 10, 2025 - For example, imagine you’re working on a PHP-based · CRM system for a small business. The system receives customer data as an object from a third-party API, and the customer’s details are relatively simple, such as their name, contact information, and status. In this case, typecasting allows you to quickly convert the object to an array, making it easier to display the data in a table or pass it to another function without the overhead of more complex methods.
Find elsewhere
🌐
Jonathan Suh
jonsuh.com › blog › convert-loop-through-json-php-javascript-arrays-objects
Convert and Loop through JSON with PHP and JavaScript Arrays/Objects — Jonathan Suh
September 30, 2014 - The second parameter accepts a boolean that when set as true, tells it to return the objects as associative arrays. You can learn more about the json_decode function from PHP’s documentation.
🌐
Wtools
wtools.io › convert-js-object-to-php-array
Convert Javascript object to PHP array Online | WTOOLS
This form allows you convert Javascript literal object to PHP Array, paste or upload your JS object file below:
🌐
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 - Using json_encode() function, PHP array can be converted to JavaScript array and accessible in JavaScript.
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-convert-php-array-to-javascript-or-json
How to convert PHP array to JavaScript or JSON ? - GeeksforGeeks
July 12, 2025 - PHP provides a json_encode() function that converts PHP arrays into JavaScript. Technically, it is in JSON format. JSON stands for JavaScript Object Notation. Statement: If you have a PHP array and you need to convert it into the JavaScript ...
🌐
Stack Overflow
stackoverflow.com › questions › 35902396 › how-can-i-convert-a-javascript-object-to-an-array
php - How can I convert a JavaScript object to an array - Stack Overflow
I have the following javascript object, in an array. I am using PHP's json_encode to get this value ... I have searched around a lot and couldn't find an answer. Any help would be really appreciated. Thanks! ... You should try to convert object and its fields to arrays and then serialize result.
🌐
GeeksforGeeks
geeksforgeeks.org › php › convert-an-object-to-associative-array-in-php
Convert an object to associative array in PHP - GeeksforGeeks
July 11, 2025 - An array that contains a string index is called an associative array. It stores element values in association with key values rather than in linear index order. ... The json_decode function accepts JSON encoded string and converts it into a PHP variable on the other hand json_encode returns a JSON encoded string for a given value. ... <?php class sample { /* Member variables */ var $var1; var $var2; function __construct( $par1, $par2 ) { $this->var1 = $par1; $this->var2 = $par2; } } // Creating the object $myObj = new sample(1000, "second"); echo "Before conversion: \n"; var_dump($myObj); // Converting object to associative array $myArray = json_decode(json_encode($myObj), true); echo "After conversion: \n"; var_dump($myArray); ?>
Top answer
1 of 16
1825

Just typecast it

$array = (array) $yourObject;

From Arrays:

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.

Example: Simple Object

$object = new StdClass;
$object->foo = 1;
$object->bar = 2;

var_dump( (array) $object );

Output:

array(2) {
  'foo' => int(1)
  'bar' => int(2)
}

Example: Complex Object

class Foo
{
    private $foo;
    protected $bar;
    public $baz;

    public function __construct()
    {
        $this->foo = 1;
        $this->bar = 2;
        $this->baz = new StdClass;
    }
}

var_dump( (array) new Foo );

Output (with \0s edited in for clarity):

array(3) {
  '\0Foo\0foo' => int(1)
  '\0*\0bar' => int(2)
  'baz' => class stdClass#2 (0) {}
}

Output with var_export instead of var_dump:

array (
  '' . "\0" . 'Foo' . "\0" . 'foo' => 1,
  '' . "\0" . '*' . "\0" . 'bar' => 2,
  'baz' =>
  stdClass::__set_state(array(
  )),
)

To know more about php built-in class stdclass. You can read this documentation

Typecasting this way will not do deep casting of the object graph and you need to apply the null bytes (as explained in the manual quote) to access any non-public attributes. So this works best when casting StdClass objects or objects with only public properties. For quick and dirty (what you asked for) it's fine.

Also see this in-depth blog post:

  • Fast PHP Object to Array conversion
2 of 16
540

You can quickly convert deeply nested objects to associative arrays by relying on the behavior of the JSON encode/decode functions:

$array = json_decode(json_encode($nested_object), true);
🌐
EDUCBA
educba.com › home › software development › software development tutorials › php tutorial › php object to array
PHP object to array | How to Convert object to array in PHP with example?
April 18, 2023 - Once this is done, the constructor takes parameters that are later offered during the object creation using the keyword “new”. From the program, it can be seen that objects get printed in the first case of expression var_dump(). But in the second case of expression, an object is casted into an array using the typecasting procedure. PHP program to convert an object to an array using json encode and json decode.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai