How to Turn a Value into an Array in PHP with (array)
(array) $value turns any value into an array in PHP: a single string becomes an array with that one string, an array stays as it is, and null becomes an empty array. It's the short way to handle data that can be either one value or a list of them.
Syntax
$<list> = (array) <value>;
| Part | What it does |
|---|---|
(array) | Converts the value that follows into an array. |
<value> | A string, a number, null, an array or an object. |
$<list> | The resulting array. |
The PHP manual: "For any of the types int, float, string, bool and resource, converting a value to an array results in an array with a single element with index zero and the value of the scalar which was converted." And: "Converting null to an array results in an empty array."
The Example
The example converts three values with (array): one audience URL as a string, a list of two URLs, and null. It prints each result with print_r, which shows an array's keys and values.
Prerequisites
- PHP. This guide uses PHP 8.6.0.
Step 1: Write the Code
Create a file named main.php:

<?php
$one = 'https://photos.example.org';
$many = ['https://photos.example.org', 'https://videos.example.org'];
print_r((array) $one);
print_r((array) $many);
print_r((array) null);
Step 2: Run It
php main.php
Array
(
[0] => https://photos.example.org
)
Array
(
[0] => https://photos.example.org
[1] => https://videos.example.org
)
Array
(
)

The command is the same in zsh, bash and PowerShell.
What Happened
- The string became an array with one element, at index
0. - The array came back unchanged.
nullbecame an empty array, so code that loops over the result simply does nothing.
That's what makes (array) handy for data like a JSON Web Token's aud claim, which can be one string or an array of strings: after (array) $claims->aud, the code only has to deal with an array.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Casting an object and expecting [$object] | You get the object's properties as array elements | Write [$object] to wrap an object |
| Casting a string and expecting its characters | The whole string becomes one element | Use str_split to split a string into characters |
Casting false and expecting an empty array | It becomes [false], an array with one element | Check for false first, or use null for "no value" |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A loop runs once over a value you expected to be a list | It was a single value, and (array) wrapped it | That's intended; check the data if it should have been a list |
| An array of property names instead of one object | An object was cast | Wrap it with [$object] |
FAQ
Is (array) the same as [$value]?
For a single scalar value, yes: the manual says (array) $scalarValue "is exactly the same as array($scalarValue)". The two differ for null, which (array) turns into an empty array, for arrays, which (array) leaves as they are, and for objects.
Does (array) change the original variable?
No. It returns a new array and leaves the variable as it was.
Conclusion
(array) $value gives you an array whatever you start with: one value is wrapped, an array stays an array, and null becomes empty.
Additional Resources
- Arrays: Converting to array, in the PHP manual.
