8

This is quite a simple question, but I am finding it hard to find an answer.

I have a script that has the following:

(array) $item->classes

I have seen array() but never (array). What does it do?

2 Answers 2

9

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

Sign up to request clarification or add additional context in comments.

Comments

3

(array) will cast an object as an array

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

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

creates an associated array equivalent to array('attribute_a' => 1, 'attribute_b' => 2).

Typecasting is not just for arrays, it works between many different types. For example an integer could be cast as a string;

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

Much more on typecasting here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.