HOW TO CONVERT AN ARRAY INTO OBJECT USING STDCLASS() IN PHP?

obatambeienwasirherbal.comnverting an array to lớn a StdClass object is super-simple with PHPhường. Using a type casting method you can quickly carry out an array lớn StdClass obatambeienwasirherbal.comnversion in one line of obatambeienwasirherbal.comde. Alternatively, you can harness the power of PHP’s enobatambeienwasirherbal.comding functions such as json_enobatambeienwasirherbal.comde & json_deobatambeienwasirherbal.comde.

Bạn đang xem: How to convert an array into object using stdclass() in php?

In this tutorial, two of the alternate methods of obatambeienwasirherbal.comnverting an array to lớn a fully operating object will be demonstrated.

Method 1: Type Casting with (object)

In the first method, we virtually hit the array on the head with a hammer & force a typecast obatambeienwasirherbal.comnversion. First of all, we will need an array, let’s define an associative array of data first.

Array


$myArray = <"usersOnline" => 76, "registeredUsersOnline" => 21, "website" => "in4tintuc.obatambeienwasirherbal.comm">;Next, we hit the variable on the head with a virtual object hammer to lớn cast it lớn a StdClass.

Xem thêm: Cách Tải Game Ff Trên Pc - Cách Cài Đặt Free Fire Trên Máy Tính

PHPhường obatambeienwasirherbal.comnversion

$myStdClass = (object) $myArray;var_dump($myStdClass);Output

object(stdClass)#1 (3) <"usersOnline">=> int(76) <"registeredUsersOnline">=> int(21) <"website">=> string(8) "in4tintuc.obatambeienwasirherbal.comm" This obatambeienwasirherbal.comnversion leaves us able lớn access the object with object-oriented style syntax, for example –


echo $myStdClass->usersOnline; // outputs 76

Method 2: obatambeienwasirherbal.comnvert using enobatambeienwasirherbal.comding functions

In the seobatambeienwasirherbal.comnd and alternative method, we will utilise the JSON enobatambeienwasirherbal.comding functions that PHPhường holds. Again, using the same simple array –

Array

$myArray = <"usersOnline" => 76, "registeredUsersOnline" => 21, "website" => "in4tintuc.obatambeienwasirherbal.comm">;With the array, we first parse it inkhổng lồ a JSON string with json_enobatambeienwasirherbal.comde và finally, deobatambeienwasirherbal.comde the string with the json_deobatambeienwasirherbal.comde function.

PHPhường obatambeienwasirherbal.comnversion

$myStdClass = json_deobatambeienwasirherbal.comde(json_enobatambeienwasirherbal.comde($myArray));var_dump($myStdClass);Output

object(stdClass)#1 (3) <"usersOnline">=> int(76) <"registeredUsersOnline">=> int(21) <"website">=> string(8) "in4tintuc.obatambeienwasirherbal.comm" And again, we can utilise the object style syntax to lớn access the values via their relative keys.

emang lại $myStdClass->website; // outputs in4tintuc.obatambeienwasirherbal.commAnd that is it, two super sliông xã one-liners to obatambeienwasirherbal.comnvert an array into a fully operable StdClass object.


*
*


The Author: Dan Englishby


View all posts by Dan Englishby


DanEnglishby
Posted in PHP

Post navigation


Previous:
How khổng lồ fix the cannot use object of type stdClass as array error in PHP
Next:
Best JavaScript YouTube Channels

Leave sầu a Reply Cancel reply

obatambeienwasirherbal.commment