Register Login

Convert PHP object to array

Updated Apr 02, 2023

When users need to store multiple elements within a single element, they use an array. We all know PHP is an Object Oriented Programming language.

An object is a specimen of class, which users use to define an individual data structure within a PHP class. PHP supports the conversion of an object into an array using the typecasting method and JSON Decode and Encode method. In this article, you will have insights into how to convert an object into an array using PHP.

What is an associative array?

An associative array indicates an array storing string values as an index. It stores element values in a combination of keys and values.

In a PHP program, when users deal with data in different formats, such as an array, string, objects, or others, they must be specific about using accurate methods.

In a real-time application, when users want to get the desired output, they may need to read the result of the PHP object in the form of an associative array. It is where the real-time application of associative array takes place.

Let us dig into the following methods to convert an object into an array using PHP:

  1. json_decode and json_encode
  2. type casting
  3. get_object_vars()

Method 1: Using json_decode and json_encode method

In this method, users will use the function json_decode that accepts a JSON encoded string and convert it into a PHP variable. On the other hand, the json_encode function returns a string after encoding it in a JSON format for the specific element.

Syntax:

$myArray = json_decode(json_encode($object), true);

Code Snippet:

<?php
class demo_class {
	var $a;
	var $b;
	
	function __construct( $x, $y )
	{
		$this->a = $x;
		$this->b = $y;
	}
}
// Here, we create the PHP object
$Obj = new demo_class(1000, "second");
echo "Let us see the object before conversion: \n";
var_dump($Obj);
// Here, we convert the object to an associative array
$Array = JSON_decode(JSON_encode($Obj), true);
echo "After we convert the object to array: \n";
var_dump($Array);
?>

Output:

Explanation:

We used the var_dump() method, which dumps all the information about the items and displays it. Then we used a JSON string available to decode it into an object and convert it into an associative array using the $obj = json_decode(json_encode($arr)) method.

This JSON_decode() method has two parameters; the second one refers to a Boolean value. If we pass true as the second parameter, then it will convert the object into an associative array, else not.

Lastly, the var_dump() method will display the items after getting converted into an associative array.

Method 2: Using type casting method

In PHP, type casting refers to using one data type for different data types. It means users explicitly use the value of a PHP variable with distinct data types.

Syntax:

$myArray = (array) $myObj;

Code Snippet:

<?php
class demo{
	var $a;
	var $b;
	var $c;
	function __construct( $x, $y, $z){
		$this->a = $x;
		$this->b = $y;
		$this->c = $z;
	}
}

$samp = new demo("This", "Is", "PHP") ;
echo "Let us see the object before conversion: " ;
var_dump($samp);
$Array = (array)$samp;
echo "After we convert the object to array: ";
var_dump($Array);
?>

Output:

Explanation:

In this code snippet, we created a class "demo" and initialized three elements, namely "a," "b," and "c." Then, we used the __construct() function, which gets executed when we create the object. Using the keyword "new," we referred to the constructor that takes the parameter declared during the object creation.

Now we can see that the method var_dump() prints the object in the first case of the expression. And in the second expression, we used the var_dump() method to convert the PHP object into an associative array using the typecasting procedure.

Note:

Also, when users use the JSON encode and decode operation, it converts the arrays into PHP object objects, taking up multiple resources if the PHP array is large. In this case, users can better use the typecasting method to typecast an array to an object.

Method 3: Using get_object_vars() method

It is an unknown method among PHP users for converting an object into an array, but it is valuable. The get_object_vars() is a built-in that allows users to get the values of the PHP object given.

Syntax:

get_object_vars( $object )

Code Snippet:

<?php
class demo {
	private $a = 1.11;
	public $b = 6;
	public $c = "Hello PHP";
	private $d;
	static $e;
	public function samp() {
		var_dump(get_object_vars($this));
	}
}
$samp = new demo;
var_dump(get_object_vars($samp));
$samp->samp();
?>

Output:

Explanation:

We used the get_object_vars() method that returns a result after converting the object into an associative array of a defined object accessible in the scope. And if this method does not find any value, it will return a NULL value.

Conclusion

It brings us to the end of the PHP article. The article has shown all the possible methods users can use to convert an object into an array associative in nature using this Object Oriented programming language.


×