How to Convert an Array to an Object?

PHP arrays are cool, but if you are switching to the modern coding standards (OOP) you are probably in need of objects more often.

PHP provides a very easy way to change the type of a variable from an array to an object. It’s done via typecasting.

Here’s an example:

'John',
	'surname'=>'Walker'
);

// The actual conversion
$obj = (object) $arr;

echo $obj->name; // Will display John
?>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.