You can cast a variable to an array by using:

    $var = (array)$arr;
Answer from cbroughton on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_casting.asp
PHP Type Casting
When casting to object, most data types converts into an object with one property, named "scalar", with the corresponding value. NULL values converts to an empty object. Indexed arrays converts to objects with the index number as property name and the value as property value.
Discussions

php - Cast to array VS is_array() - Stack Overflow
Does anyone know of any issues, performance or other, that can occur by casting a variable to an array instead checking it first? // $v could be a array or string $v = array('1','2','3'); OR $v... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Why does PHP convert integer-string array keys to integer?
The thing is that PHP is used for web stuff, thus all input is coming as string, be it numeric or textual (or binary) data. Having those auto conversions makes that simple. Nowadays the language evolved and frameworks and type handling changed things, but such fundamental issues can not be changed without breaking "everything" Just a few examples what becomes simpler: Given a script like More on reddit.com
๐ŸŒ r/PHP
17
16
September 4, 2022
Warning: Array to string conversion in Variable method
What if you try $router->{$value['method']}( $value['path'], 'something' ); This is also how you should use array items within double quoted strings, think of it as how you use parenthesis in a math formula, to specify what parts go together. With your original code, PHP can't determine if $router->$value is an array and you are trying to access the 'method' element from that (what it seems to be assuming) versus that you are trying to use the 'method' element of value. wrapping it with the curly braces, ensures it knows you mean the later. Edit: fixed typos More on reddit.com
๐ŸŒ r/PHPhelp
6
2
May 10, 2024
Using implode() on an array of booleans will cast TRUE to 1 and FALSE to ''
implode() is a function for making an array of strings into a single string with separators. If you give it an array with elements that aren't strings, they're converted to strings. This appears to use PHP's normal conversion rules. There's nothing wrong with implode() itself. More on reddit.com
๐ŸŒ r/lolphp
18
0
November 20, 2015
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
๐ŸŒ
Exakat
exakat.io โ€บ home โ€บ mastering the (array) cast operator in php
Mastering the (array) Cast Operator in PHP - Exakat
December 3, 2023 - Instead of setting the value in an array, PHP converts null to the empty array(). Technically, it is an array with nothing in it, just like nullrepresents nothing. If you need that value to be stored in an array, then you need to put it manually in the array, like for nesting arrays in arrays. One of the reason to cast values to array is to handle all elements of a list of objects in the same way, may they be zero, one or more.
๐ŸŒ
GitHub
gist.github.com โ€บ victorbstan โ€บ 744478
recursively cast a PHP object to array ยท GitHub
Use ArrayObject class and cast it to array (or use ArrayObject::getArrayCopy() instead). It's much much faster. php > $x = [1, (object) ['x'=>'y'], 3]; php > var_dump($x); array(3) { [0] => int(1) [1] => class stdClass#1 (1) { public $x => string(1) ...
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ language.types.type-juggling.php
PHP: Type Juggling - Manual
It may not be obvious exactly what will happen when casting between certain types. For more information, see these sections: ... Because PHP supports indexing into strings via offsets using the same syntax as array indexing, the following example holds true for all PHP versions: ... See the section titled String access by character for more information. Learn How To ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ php-how-to-cast-variable-to-array
PHP How to cast variable to array?
March 15, 2026 - This is useful when you need to ... = (array)$nameArray; print_r($valueArray); ?> ... Array casting in PHP converts any variable type to an array....
๐ŸŒ
Jasonmooberry
jasonmooberry.com โ€บ log โ€บ articles โ€บ PHP-cast-as-array
PHP cast as array
June 27, 2010 - Flexibility in signature params, ... question marks in a string with supplied string(s). By casting $elements as an array we can take a single param or an array of params without checking for it in our code....
Find elsewhere
๐ŸŒ
GitHub
ocramius.github.io โ€บ blog โ€บ fast-php-object-to-array-conversion
Fast PHP Object to Array conversion
A couple of months ago, I found a forgotten feature of PHP itself. Apparently, it is possible to cast objects to arrays like following:
๐ŸŒ
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. Here, the first method in the method sections is used for converting an object into an array. An associative array is an array that consists of a string index which stores item values linked with key values other than in order of the linear index. This article saw how PHP object to array is working, methods to achieve it, and different examples.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
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 - 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, ...
๐ŸŒ
Eric
eric.blog โ€บ 2018 โ€บ 08 โ€บ 05 โ€บ recursively-cast-to-array-in-php
Recursively cast to array in PHP โ€“ Eric Binnion
August 5, 2018 - return array_map( __FUNCTION__, (array) $input ); Just to play nicely with namespaces. ... This site uses Akismet to reduce spam.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ convert-an-object-to-associative-array-in-php
Convert an object to associative array in PHP - GeeksforGeeks
July 11, 2025 - Before conversion: object(sample)#1 ... conversion of a data type. It can convert a PHP object to an array by using typecasting rules supported in PHP....
๐ŸŒ
Edureka
edureka.co โ€บ blog โ€บ convert-object-to-array-in-php
How To Convert Object To Array In PHP | PHP Fundamentals | Edureka
September 20, 2019 - Type Casting object to an array ... is simply the explicit conversion of a data type. By using typecasting rules supported in PHP, it will convert a PHP object to an array....
๐ŸŒ
PHP
wiki.php.net โ€บ rfc โ€บ to-array
PHP: rfc:to-array
August 28, 2019 - Adding a __toArray() method gives developers the ability to transform a class to an array in similar fashion. ... class Person { protected $name; protected $email; public $foo = 'bar'; public function __construct(string $name, string $email) { $this->name = $name; $this->email = $email; } public ...
๐ŸŒ
PHP Tutorial
techkubo.com โ€บ home โ€บ php casting
PHP Casting - PHP Tutorial
May 23, 2025 - To cast a variable to an array, use the (array) statement:
๐ŸŒ
Medium
medium.com โ€บ @mavelar โ€บ convert-array-to-object-object-to-array-in-php-e430ae492951
Convert Array to Object & Object to Array in PHP | by Manuel Avelar | Medium
September 28, 2017 - If you ever need to change an Object into an Array, then use the (array) type casting function. function object_to_array($object) { return (array) $object; }
๐ŸŒ
Envato Tuts+
code.tutsplus.com โ€บ home โ€บ php
PHP Type Casting: A Complete Guide | Envato Tuts+
November 30, 2021 - The conventions followed by PHP when converting anything to a string are listed below. A false boolean value becomes an empty string, and a true value becomes the string "1". Integers and floats are converted to a textual representation of those numbers. All arrays are converted to the string "Array".