Php objects in associative array

How to Convert an Object to Associative Array in PHP

Here are the two ways to convert an object to an array in PHP.

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

Method 1: Using the json_encode() and json_decode() method

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

Syntax

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

Example

  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); 
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" >

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

Method 2: 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

Example

  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 one line from the above json_encode() code.

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

Get the properties of the given object in PHP

The get_object_vars() function will give us all the object’s properties.

get_object_vars (object $object)
 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 in both instances, and private and protected will depend on the context. See the output.

array(2)  ["y"]=> int(11) ["z"]=> int(21) > array(4)  ["x"]=> int(19) ["y"]=> int(11) ["z"]=> int(21) ["e"]=> int(29) > 

Источник

Convert PHP Object to Associative Array

Convert PHP Object to Associative Array

  1. Use the array Keyword to Typecast the StdClass ’s Object to Convert Into an Associative Array in PHP
  2. Use the StdClass ’s Object Inside of a User-Defined Class to Convert the Object Into an Associative Array in PHP
  3. Use the json_encode() and json_decode() Functions to Convert the Object Into an Associative Array in PHP

We will introduce a method to convert the PHP object to an associative array typecasting the objects of StdClass using the array keyword. We will use the var_dump() function to display the associative array.

The second approach demonstrates another method to convert the PHP object into an associative array creating a StdClass in the constructor of a user-defined class. We will convert the object into the associative array as in the first method using the array keyword. This method follows the object-oriented approach.

We will also introduce another method to convert the object into associative array in PHP using the json_encode() and json_decode() functions. We will use the StdClass to create the object and the dynamic properties.

Use the array Keyword to Typecast the StdClass ’s Object to Convert Into an Associative Array in PHP

We can create the StdClass empty class to create an object in PHP and use the object to create properties. The object of the class can directly access the properties. It can also create dynamic properties for the class. We can use the array object to typecast the object into an array. The var_dump() function dumps the information about the array’s type and values.

For example, create an object $object of the StdClass using the new operator. Create two properties using the $object named car1 and car2 . Assign the properties with the values porsche and bugatti . Use the array keyword to typecast the $object variable. Wrap the array keyword with parenthesis before the $object variable and dump the value using the var_dump() function. The example below converts the objects into an associative array, as shown in the output section. It shows the key and value pairs for each element of the array.

#php 7.x  php $object = new StdClass; $object->car1 = "porsche"; $object->car2 = "bugatti"; var_dump( (array) $object ); ?> 
array(2) < ["car1"]=>string(7) "porsche" ["car2"]=> string(7) "bugatti" > 

Use the StdClass ’s Object Inside of a User-Defined Class to Convert the Object Into an Associative Array in PHP

We can create an instance of the StdClass in the class’s constructor and use the array keyword to convert the classes’ object to an associative array. We can create a class and define some properties of it. The constructor of the class initializes the properties of the class to specific values. We can create an object of the StdClass and assign it with one of the properties of the class. The new operator, along with the class, invokes the constructor. We can use the array keyword right before the invocation to convert the object of the class to an associative array.

For example, create a class Motorcycle . Create three class properties with the private access modifier as $name , $color , and $type . Create a constructor of the class and inside the constructor initialize the values of the properties. Write Husky for the name , white for color and create an object of the StdClass for type . Use the $this keyword to initialize the properties. Outside the class, invoke the Motorcycle class and use the array keyword to typecast before invoking. Use the var_dump() function to dump the information about the typecasted array.

#php 7.x  class Motorcycle  private $name;  private $color;  private $type;  public function __construct()  $this->name = "Husky";  $this->color = "white";  $this->type = new StdClass;  > > var_dump( (array) new Motorcycle ); 
array(3) < ["Motorcyclename"]=>string(5) "Husky" ["Motorcyclecolor"]=> string(5) "white" ["Motorcycletype"]=> object(stdClass)#2 (0) < >> 

Use the json_encode() and json_decode() Functions to Convert the Object Into an Associative Array in PHP

The json_encode() function encodes a value to the JSON object and the json_decode() function converts the JSON object to the PHP object. The boolean value, the second parameter of the json_decode() function, indicates what the JSON object should be converted. The true value will convert the JSON object to an associative array, while the false value will convert it into a PHP object.

For example, create an object of the StdClass and assign it to the $object variable. Name two values of the object property as Mustang and Manang and store them in place1 and place2 variable. Use the json_encode() function on the $object variable and store the value in $json variable. Then, use the json_decode() function on the $json variable and use the boolean value true as the second parameter. Store the value in $array variable. Dump the variable using the var_dump() function.

In the example below, the variable $object holds the StdClass object. The json_ecode() function converts the object into the JSON string. The json_decode() function converts the JSON string into the associative array.

#php 7.x  php $object = new StdClass; $object->place1 = "Mustang"; $object->place2 = "Manang"; $json= json_encode($object); $array = json_decode($json, true); var_dump($array); ?> 
array(2) < ["place1"]=>string(7) "Mustang" ["place2"]=> string(6) "Manang" > 

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

Related Article — PHP Object

Источник

How to Convert a PHP Object to an Associative Array

This snippet will explain the ways of converting a PHP object to an associative array.

Below, you can find efficient ways to meet that goal.

Using json_decode and json_encode

The first method is applying json_decode and json_encode functions. The first function is capable of accepting JSON-encoded string and converting it into a PHP variable. The second one returns a JSON-encoded string for a particular value.

The syntax will look as follows:

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

For a better perception, check out the following example:

 class sample < /* Member variables */ var $var1; var $var2; function __construct($par1, $par2) < $this->var1 = $par1; $this->var2 = $par2; > > // Creating the object $myObj = new sample(1000, "second"); echo "Before conversion: \n"; var_dump($myObj); // Converting object to associative array $myArray = json_decode(json_encode($myObj), true); echo "After conversion: \n"; var_dump($myArray); ?>

Its outcome looks like this:

Before conversion: object(sample)#1 (2) < ["var1"]=>int(1000) ["var2"]=> string(6) "second" > After conversion: array(2) < ["var1"]=>int(1000) ["var2"]=> string(6) "second" >

Casting Object to an Array

The second method we recommend is casting the object to an array. Normally, typecasting considers the utilization of one data type variable to the other data type. Simply, it is the explicit conversion of a data type. With it, one can convert a PHP object into an array with the help of type casting rules.

The example will look like this:

 class bag < /* Member variables */ var $item1; var $item2; var $item3; function __construct($par1, $par2, $par3) < $this->item1 = $par1; $this->item2 = $par2; $this->item3 = $par3; > > // Create myBag object $myBag = new bag("Mobile", "Charger", "Cable"); echo "Before conversion : \n"; var_dump($myBag); // Coverting object to an array $myBagArray = (array) $myBag; echo "After conversion : \n"; var_dump($myBagArray); ?>

The output will look as follows:

Before conversion : object(bag)#1 (3) < ["item1"]=>string(6) "Mobile" ["item2"]=> string(7) "Charger" ["item3"]=> string(5) "Cable" > After conversion : array(3) < ["item1"]=>string(6) "Mobile" ["item2"]=> string(7) "Charger" ["item3"]=> string(5) "Cable" >

What is an Associative Array

An object is considered an instance of a class. It is the base for a class and has allocated memory.

The data structure is capable of storing one or more similar type of values in a single name. The associative array is something different. An associative array is considered an array, containing string index.

Источник

Читайте также:  Data structures and algorithms in java лафоре
Оцените статью