top of page
Writer's pictureThe Tech Platform

Convert PHP object to associative array

During PHP development, you may never come across the need for this functionality, but its an extremely good piece of knowledge to have within your arsenal. If you’ve been using the Laravel PHP framework lately, this will be highly relevant to your development work.


Now, I don’t know about you, but within PHP, I detest reading objects, I’d much rather have them as associative array’s. I don’t know why exactly, but that’s just me.


2 PHP Object to Associative Array Solutions


Solution 1 – Using json_decode & json_encode

Here is the nice, quick, one line magical piece of code that converts your object to an associative array. With the following line of code, replace the $object variable with the name of your object variable you want to convert.

$myAssociativeArray = json_decode(json_encode($object), true);// Converts the object to an associative array

The $object variable above contained the following when using var_dump($object) –

object(stdClass)[1]
public 'hexBlack' => string '#000000' (length=7)
public 'hexRed' => string '#FF0000' (length=7)
public 'hexMaroon' => string '#800000' (length=7)
public 'hexYellow' => string '#FFFF00' (length=7)

Meaning that you can access the values like this –

echo $object->hexBlack; // outputs #000000

And after the conversion took place, the $myAssociativeArray contained the following when using var_dump($myAssociativeArray)-

array (size=4)'hexBlack' => string 
'#000000' (length=7)'hexRed' => string 
'#FF0000' (length=7)'hexMaroon' => string '#800000' (length=7)
'hexYellow' => string '#FFFF00' (length=7)

Meaning you can now access it nicely like so –


echo $myAssociativeArray['hexBlack']; // outputs #000000


Solution 2 – Type Casting object to an array

For the most part, the following section of this article is pretty repetitive, but the first line of code below is the most important and should be noted. It rapidly typecasts an object to an array format, it’s nice and easy on the eye and does the job evidently well.

$myAssociativeArray = (array) 
$carObject; // Converts the object to an associative array

The $object variable above contained the following when using var_dump($carObject) –

object(stdClass)[1]
public 'CarBrand1' => string 'Ford' (length=4)
public 'CarBrand2' => string 'Volvo' (length=5)
public 'CarBrand3' => string 'Renault' (length=7)
public 'CarBrand4' => string 'Porsche' (length=7)Copy

Meaning that you can access the values like this –

echo 
$carObject->CarBrand1; // outputs 
Ford

And after the conversion took place, the $myAssociativeArray contained the following when using var_dump($myAssociativeArray)-

array (size=4)'CarBrand1' => string 
'Ford' (length=4)'CarBrand2' => string 
'Volvo' (length=5)'CarBrand3' => string 
'Renault' (length=7)'CarBrand4' => string 'Porsche' (length=7)

Meaning you can now access it nicely like so –

echo 
$myAssociativeArray['hexBlack']; 
// outputs 
#000000

Here is the solution 1 whole code to play with if you want to dig into the nitty-gritty.

$object = (object) 
[
'hexBlack' => '#000000',
'hexRed' => '#FF0000',
'hexMaroon' => '#800000',
'hexYellow' => '#FFFF00'];
echo 
$object->hexBlack; // outputs 
#000000var_dump($object);
$myAssociativeArray = json_decode(json_encode($object), true);
var_dump($myAssociativeArray);
echo 
$myAssociativeArray['hexBlack'];


Summary

Hope these two solutions will save you some frustration and open a new door to how you want to read your data during looping or simply ‘static’ calls to the data itself.


Source: codewall

0 comments

コメント


bottom of page