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 TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Now, let us see different ways in which we can convert PHP object to array.
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);
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;
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.
When the object is var_dump, all items will be displayed.
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.
Different examples are mentioned below:
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:
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.
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:
In diesem Programm wird auch eine Klasse Krankenhaus erstellt und darin werden zwei Elemente wie el1 und el2 erstellt. Anschließend wird eine __construct()-Funktion deklariert, die während der Objekterstellung ausgeführt wird. Sobald dies erledigt ist, übernimmt der Konstruktor Parameter, die später bei der Objekterstellung mit dem Schlüsselwort „new“ angeboten werden. Aus dem Programm ist ersichtlich, dass Objekte im ersten Fall des Ausdrucks var_dump() gedruckt werden. Aber im zweiten Fall des Ausdrucks wird ein Objekt mithilfe der Typumwandlungsprozedur in ein Array umgewandelt. Hier wird die erste Methode in den Methodenabschnitten zum Konvertieren eines Objekts in ein Array verwendet.
Ein assoziatives Array ist ein Array, das aus einem String-Index besteht, der Elementwerte speichert, die mit Schlüsselwerten in einer anderen Reihenfolge als dem linearen Index verknüpft sind. In diesem Artikel wurde erläutert, wie PHP-Objekte in Arrays funktionieren, welche Methoden es gibt und welche Beispiele es gibt.
Das obige ist der detaillierte Inhalt vonPHP-Objekt zum Array. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!