Home  >  Article  >  Backend Development  >  PHP object to array

PHP object to array

WBOY
WBOYOriginal
2024-08-29 12:43:59879browse

The following article provides an outline for PHP object to array. As we all know, object is known as a class instance which has memory allocated. In the case of an array, it is a data structure containing one or more values of a similar type in a single name. On the other hand, an associative array is not like a normal PHP array. An associative array is an array that consists of a string index which stores item values linked with key values other than in order of the linear index.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Methods of a PHP object to array

Now, let us see different ways in which we can convert PHP object to array.

Method 1

With the help of the json_decode and json_encode method

In this method, the function json_decode takes JSON encoded string and changes it into a PHP variable, whereas the json_encode function returns a string which is encoded in a json format for a particular value.

Syntax:

$arr = json_decode(json_encode ( $obj ) , true);

Method 2

With the help of type casting

Typecasting is a technique in which one data type variable into the another data type. It is considered as an explicit data type conversion. It can translate a PHP object to an array with the help of typecasting rules in PHP.

Syntax:

$arr = (array) $obj;

How to Convert an object to an array in PHP?

As we all know, there are several formats of data like strings, objects, arrays etc. In the case of PHP also, there are data formats like this. In order to get the needed output, a php object obj result is needed in a format of an associative array.

Now, let us see how to translate a php object.

Code:

<?php
class hospital
{
// elements
. . . .
function __construct( $dis1, $dis2, $dis3)
{
// Use this pointer
. . . .
}
// create class object
. . .
// convert object to array
. . . .
?>

This is the skeleton for converting an object into an array.

Now let us see how to perform this.

  • In order to encode the string, use “object = json_encode($array);”

When the object is var_dump, all items will be displayed.

  • For decoding into an object, a json string which is available will be used to convert and string formatting is done to an object. It will be done using $obj = json_decode(json_encode($arr));
  • When the object is var_dump, all items will be displayed after converting into an array.

Here, one important point to consider is json_decode that translates a json string to an object, except you offer another option that is boolean which can be true or false. Even if the second parameter is considered as true, an array will be obtained.

Also, when json encode operation and decode operation are used, arrays are converted to objects that take up many resources if the array is large. In that case, the better method to type cast an array to an object that uses the object cast.

Consider $obj = (object) $arr; syntax. Here also, object will be converted into arrays.

Based on the requirements, you can choose the method you want for the conversion of an array into an object in PHP.

Examples of a PHP object to array

Different examples are mentioned below:

Example #1

PHP program to convert an object to an array using the typecasting method.

Code:

<?php
class hospital
{
var $el1;
var $el2;
var $el3;
function __construct( $dis1, $dis2, $dis3)
{
$this->item1 = $dis1;
$this->item2 = $dis2;
$this->item3 = $dis3;
}
}
// Creation of object for the class
$dis = new hospital("D", "S", "C") ;
echo "Items before conversion : " ;
var_dump($dis);
// convert object to array
$arr = (array)$dis;
echo "Items after conversion : ";
var_dump($arr);
?>

Output:

PHP object to array

In this program, a class hospital is created, and inside that, three elements such as el1, el2, and el3. Then, a __construct() function is declared, which gets executed during the time object is created. Once this is done, the constructor takes parameters that are later offered during the object creation using the keyword “new”. From the program, it can be seen that objects get printed in the first case of expression var_dump(). But in the second case of expression, an object is casted into an array using the typecasting procedure.

Example #2

PHP program to convert an object to an array using json encode and json decode.

Code:

<?php
class hospital
{
var $el1;
var $el2;
function __construct( $dis1, $dis2 )
{
$this->item1 = $dis1;
$this->item2 = $dis2;
}
}
// Creating object
$dis = new hospital(500, "C");
echo "Items before conversion : " ;
var_dump($dis);
// convert object to array
$arr = json_decode(json_encode($dis), true);
echo "Items after conversion : ";
var_dump($arr);
?>

Output:

PHP object to array

In this program also, a class hospital is created, and inside that, two elements such as el1 and el2, are created. Then, a __construct() function is declared, which gets executed during the time object is created. Once this is done, the constructor takes parameters that are later offered during the object creation using the keyword “new”. From the program, it can be seen that objects get printed in the first case of expression var_dump(). But in the second case of expression, an object is casted into an array using the typecasting procedure. Here, the first method in the method sections is used for converting an object into an array.

Conclusion

An associative array is an array that consists of a string index which stores item values linked with key values other than in order of the linear index. This article saw how PHP object to array is working, methods to achieve it, and different examples.

The above is the detailed content of PHP object to array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn