AppDividend
Latest Code Tutorials

How to Convert PHP Object to Associative Array in PHP

An object is an instance of a class. It is merely a specimen of a class and has memory allocated. The array is the data structure that stores one or more similar types of values in a single name, but an associative array is different from a simple PHP array.

The array which contains the string index is called an associative array. This saves element values associated with key values rather than in the linear index order.

How to Convert PHP Object to Associative Array

To convert PHP object to array:

  1. Using json_encode() and json_decode() method.
  2. Type Casting object to an array.

Using json_encode() and json_decode() method

We can convert the PHP Object to an associative array using json_encode and json_decode methods.

See the following code.

<?php 
class ST { 
      
    function __construct($mike, $eleven)  
    { 
        $this->var1 = $mike; 
        $this->var2 = $eleven; 
    } 
} 
  
// Creating the object 
$st3 = new ST('Finn', 'Millie'); 
echo "Before conversion: \n"; 
var_dump($st3); 
  
// Converting object to associative array 
$arr = json_decode(json_encode($st3), true); 
echo "After conversion: \n"; 
var_dump($arr);

We have defined one class called ST, and in that class, we have used the constructor to set the variable when we create an object.

Then we create an object and pass the two parameters to the constructors. Then we are using json_encode() and json_decode() methods to convert an object to the associative array.

See the output.

➜  pro php app.php
Before conversion:
object(ST)#1 (2) {
  ["var1"]=>
  string(4) "Finn"
  ["var2"]=>
  string(6) "Millie"
}
After conversion:
array(2) {
  ["var1"]=>
  string(4) "Finn"
  ["var2"]=>
  string(6) "Millie"
}
➜  pro

You can quickly convert deeply nested objects to associative arrays by relying on the behavior of the JSON encode/decode functions.

Type Casting object to an array

Typecasting is the way to utilize one data type variable into the different data types, and it is merely the explicit conversion of a data type. For example, it can convert a PHP object to an array using typecasting rules supported in PHP.

Syntax

See the syntax of typecasting.

$arr = (array) $obj;

See the following code.

<?php 
class ST { 
      
    function __construct($mike, $eleven)  
    { 
        $this->var1 = $mike; 
        $this->var2 = $eleven; 
    } 
} 
  
// Creating the object 
$st3 = new ST('Finn', 'Millie'); 
echo "Before conversion: \n"; 
var_dump($st3); 
  
// Converting object to associative array 
$arr = (array) $st3; 
echo "After conversion: \n"; 
var_dump($arr);

We have to change the one line following from the above json_encode() code.

$arr = (array) $st3;

Now, see the output.

➜  pro php app.php
Before conversion:
object(ST)#1 (2) {
  ["var1"]=>
  string(4) "Finn"
  ["var2"]=>
  string(6) "Millie"
}
After conversion:
array(2) {
  ["var1"]=>
  string(4) "Finn"
  ["var2"]=>
  string(6) "Millie"
}
➜  pro

Get the properties of the given object in PHP

The get_object_vars() function will provide us with all the given object’s properties.

get_object_vars (object $object)

See the code example.

<?php

class Data {
  private $x = 19;
  public $y = 11;
  public $z = 21;
  private $e = 29;
  static $m = 46;
 
  public function test() {
      var_dump(get_object_vars($this));
  }
}

$duh = new Data;
var_dump(get_object_vars($duh));

$duh->test();

In the above code, we will get two different outputs.

We will get only the public values on both instances, and private and protected will depend on the context. See the output.

➜  pro php app.php
array(2) {
  ["y"]=>
  int(11)
  ["z"]=>
  int(21)
}
array(4) {
  ["x"]=>
  int(19)
  ["y"]=>
  int(11)
  ["z"]=>
  int(21)
  ["e"]=>
  int(29)
}
➜  pro

That’s it for this tutorial.

Recommended Posts

  1. PHP Functions
  2. PHP str_replace()
  3. PHP explode()
  4. PHP array_replace()
  5. PHP array_flip()
Leave A Reply

Your email address will not be published.

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