Skip to main content

How to Turn a Value into an Array in PHP with (array)

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

(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​

Syntax
$<list> = (array) <value>;
PartWhat 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:

main.php, which casts a single URL string, an array of two URLs and null with (array) and prints each result with print_r.

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​

Terminal
php main.php
Output
Array
(
[0] => https://photos.example.org
)
Array
(
[0] => https://photos.example.org
[1] => https://videos.example.org
)
Array
(
)

A zsh terminal where php main.php prints an array with one URL at index 0, an array with the two URLs at indexes 0 and 1, and an empty 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.
  • null became 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​

MistakeWhat happensDo this instead
Casting an object and expecting [$object]You get the object's properties as array elementsWrite [$object] to wrap an object
Casting a string and expecting its charactersThe whole string becomes one elementUse str_split to split a string into characters
Casting false and expecting an empty arrayIt becomes [false], an array with one elementCheck for false first, or use null for "no value"

Troubleshooting​

SymptomLikely causeFix
A loop runs once over a value you expected to be a listIt was a single value, and (array) wrapped itThat's intended; check the data if it should have been a list
An array of property names instead of one objectAn object was castWrap 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​