0

I've seen code that takes a SimpleXMLElement object and casts it to array, eg.

$sxe = simplexml_load_file($file);
$arr = (array)$sxe;

but I can't find any PHP documentation for the (array) part.

Can anyone point me in the right direction? Thanks.

2 Answers 2

1

The (array) notation is just a cast - in PHP jargon it's called Type Juggling

In this case, it casts the response of simplexml_load_file (which is of type object) to an array.

What happens in this case is described in this section of the PHP documentation:

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, [...]

I recommend you to output the casted array and just see what you've got

var_dump($arr)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I mistakenly thought casting to array would be done by the object itself. Thanks for the clarification and especially the link.
0

I wouldn't suggest using standart PHP ways for that. Instead I would recommend using serializer library. It is quite easy to use IMO.

It allows different types of conversions. I myself used it for Xml => Object binding. Works as a charm. But as fas as I know it also allows such conversion type, that you need.

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.