I think every PHP coders have come accross Arrays and stdClass Objects (belongs to PHP Predefined Classes). Sometimes it’s very useful convert Objects to Arrays and Arrays to Objects. This is easy if arrays and objects are one-dimensional, but might be little tricky if using multidimensional arrays and objects.
This post defines two ultra simple recursive function to convert multidimensional Objects to Arrays and multidimensional Arrays to Objects.
Function to Convert stdClass Objects to Multidimensional Arrays
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php function objectToArray($d) { if (is_object($d)) { // Gets the properties of the given object // with get_object_vars function $d = get_object_vars($d); } if (is_array($d)) { /* * Return array converted to object * Using __FUNCTION__ (Magic constant) * for recursive call */ return array_map(__FUNCTION__, $d); } else { // Return array return $d; } } |
Function to Convert Multidimensional Arrays to stdClass Objects
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php function arrayToObject($d) { if (is_array($d)) { /* * Return array converted to object * Using __FUNCTION__ (Magic constant) * for recursive call */ return (object) array_map(__FUNCTION__, $d); } else { // Return object return $d; } } |
Function usage
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Create new stdClass Object $init = new stdClass; // Add some test data $init->foo = "Test data"; $init->bar = new stdClass; $init->bar->baaz = "Testing"; $init->bar->fooz = new stdClass; $init->bar->fooz->baz = "Testing again"; $init->foox = "Just test"; // Convert array to object and then object back to array $array = objectToArray($init); $object = arrayToObject($array); // Print objects and array print_r($init); echo "\n"; print_r($array); echo "\n"; print_r($object); |
Test output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
stdClass Object ( [foo] => Test data [bar] => stdClass Object ( [baaz] => Testing [fooz] => stdClass Object ( [baz] => Testing again ) ) [foox] => Just test ) Array ( [foo] => Test data [bar] => Array ( [baaz] => Testing [fooz] => Array ( [baz] => Testing again ) ) [foox] => Just test ) stdClass Object ( [foo] => Test data [bar] => stdClass Object ( [baaz] => Testing [fooz] => stdClass Object ( [baz] => Testing again ) ) [foox] => Just test ) |
Your post is very helpful, Thank’s! Could you please change a website’s font. I have broken my eyes
hey dear,
sorry but you can use casting in php it’s easier
$object = (object) $array;
// now $object has array elements as object;
$array = (array) $object;
// now $array has everything in $object as an array
and thanks for listening :D
Hi Alaa M. Jaddou,
Try following:
And
Then you can see that casting is not working!? Just tried it on PHP 7.0.6.
A simpler way to do this would be:
$myArray = json_decode( json_encode($myObject), true);
The second argument for json_decode tells json_decode to use arrays. This has the added benefit of allowing for arrays in the objects.