PHP and MySQL (56 Blogs) Become a Certified Professional

How To Convert Object To Array In PHP?

Published on Sep 20,2019 88.1K Views

How To Convert Object To Array In PHP?

An individual instance of the data structure which is defined by a class is an Object. We also name objects as instances. Generally, we define a class once and then make many objects that belong to it. One of the data structures that stores one or more similar types of values in a single name is an array but associative array in PHP is something different from a simple PHP array. Associative arrays are generally used to store key-value pairs. In this article we will learn ‘How To Convert Object To Array In PHP?’

Following pointers will be covered in this article,

So let us get started then

How To Convert Object To Array In PHP?

Type Casting object to an array

In order to utilize one data type variable into different data types, we can use typecasting which is simply the explicit conversion of a data type. By using typecasting rules supported in PHP,

it will convert a PHP object to an array.

Syntax: $Array_var = (array) $Obj;

Below example demonstrates the working of typecasting object to an array in PHP

<?php
class hotel
{
var $item1;
var $item2;
var $item3;
function __construct( $food1, $food2, $food3)
{
$this->item1 = $food1;
$this->item2 = $food2;
$this->item3 = $food3;
}
}
// Create object for class(hotel)
$food = new hotel("biriyani", "burger", "pizza");
echo "Before conversion : ";
echo "<br>";
var_dump($food);
echo "<br>";
// Coverting object to an array
$foodArray = (array)$food;
echo "After conversion :";
var_dump($foodArray);
?>

Moving on with this article on how to convert Object to Array In PHP?

Using Json Decode & Json Encode

JSON encoded string is accepted by json_decode function and converts it into a PHP variable and on the other hand, JSON encoded string for a given value is returned by json_encode

Syntax: $Array_var = json_decode(json_encode($obj), true)

Below example demonstrates the conversion of object to array in PHP using json_decode & json_encode.

<?php
class hotel
{
var $var1;
var $var2;
function __construct( $bill, $food )
{
$this->var1 = $bill;
$this->var2 = $food;
}
}
// Creating object
$food = new hotel(500, "biriyani");
echo "Before conversion:";
echo "<br>";
var_dump($food);
echo "<br>";
// Converting object to associative array
$foodArray = json_decode(json_encode($food), true);
echo "After conversion:";
var_dump($foodArray);
?>

This brings us to the end of this article on How To Convert Object To Array In PHP.

If you found this PHP article relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of ”How To Convert Object To Array In PHP” article and I will get back to you.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Convert Object To Array In PHP?

edureka.co