How to Convert an Array to an Object?

How to Convert an Array to an Object?

Dzhuneyt Ahmed - Author of this post

By: Dzhuneyt Ahmed

Posted · 1 min read

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 ?>

Dzhuneyt Ahmed

Dzhuneyt

Helping teams build reliable cloud infrastructure — without the bloated bill.

Social

My Other Blogs

© 2026 Dzhuneyt Ahmed. All rights reserved.