AppDividend
Latest Code Tutorials

How To Convert PHP Object To Associative Array In PHP

0

How To Convert PHP Object To Associative Array In PHP is today’s topic. 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 type of values in a single name, but associative array is different from a simple PHP array. The array which contains the string index is called an associative array. It saves element values in association with key values rather than in the linear index order.

How To Convert PHP Object To Associative Array

We can convert using two methods.

  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 are creating 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 type, and it is merely the explicit conversion of a data type.

It can convert a PHP object to an array by using typecasting rules supported in PHP.

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 which is 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 properties of the given object.

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 output.

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

Finally, Convert PHP Object To Associative Array In PHP is over.

Recommended Posts

  1. PHP Functions Tutorial With Example | Functions In PHP
  2. PHP str_replace Example | PHP str_replace() Function Tutorial
  3. PHP Explode Function Example | PHP String Explode Tutorial
  4. PHP Array Replace Example | array_replace() Function Tutorial
  5. PHP Array Flip Example | array_flip() Function Tutorial

Leave A Reply

Your email address will not be published.

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