An array enclosed in parentheses in PHP (array)

This is a pretty simple question, but it's hard for me to find the answer.

I have a script that has the following:

(array) $item->classes 

I saw array() but never (array) . What is he doing?

+7
source share
2 answers

This is called typecasting. You can read more about PHP documentation . (array) used to convert scalar or object to array see Convert to array

+9
source

(array) will return an object as an array

Assuming $item->classes->attribute_a = 1 and $item->classes->attribute_b = 2 ,

 $object_to_array = (array)$item->classes; 

creates a linked array equivalent to array('attribute_a' => 1, 'attribute_b' => 2) .

Typecasting is intended not only for arrays, but also for different types. For example, an integer can be represented as a string;

 $i = 123; $string_i = (string)$i; 

Significantly more on typginging here

+2
source

All Articles