<?php | |
/* | |
This function saved my life. | |
found on: http://www.sitepoint.com/forums//showthread.php?t=438748 | |
by: crvandyke | |
It takes an object, and when all else if/else/recursive functions fail to convert the object into an associative array, this one goes for the kill. Who would'a thunk it?! | |
*/ | |
$array = json_decode(json_encode($object), true); |
This comment has been minimized.
This comment has been minimized.
Exactly what I was looking for. Give this man a beer. |
This comment has been minimized.
This comment has been minimized.
Awesome! |
This comment has been minimized.
This comment has been minimized.
Just tried over some nested objects/array and it didn't work. `private function object_to_array($obj) { //permet de changer les private en public, pour un meuilleur conversion des object en array |
This comment has been minimized.
This comment has been minimized.
@TwanoO67 I had the same issue when converting an array to object using the code below. It only converted the outer most array to object and not the nested arrays.
I solved this by using:
I'm pretty sure the solution would be similar when converting from object to array. |
This comment has been minimized.
This comment has been minimized.
Awesome! |
This comment has been minimized.
This comment has been minimized.
Mine too! |
This comment has been minimized.
This comment has been minimized.
This will not be be applicable if the properties of the object you are trying to convert are declared as private |
This comment has been minimized.
This comment has been minimized.
@mdeboer Thank you
|
This comment has been minimized.
This comment has been minimized.
Thanks! |
This comment has been minimized.
This comment has been minimized.
Nice! |
This comment has been minimized.
This comment has been minimized.
@victorbstan thanks man. |
This comment has been minimized.
This comment has been minimized.
@mdeboer Thank you |
This comment has been minimized.
This comment has been minimized.
I love the simplicity. Thank you! |
This comment has been minimized.
This comment has been minimized.
am i the only one here that minds the "wasted" cycles of json encoding + decoding? |
This comment has been minimized.
This comment has been minimized.
In case of complex objects, you will have serious performance issues with |
This comment has been minimized.
This comment has been minimized.
Use this function to access also private and protected arguments function obj2arr( $obj, $nestLevel = 0 ) {
} |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Be care full if use encodings other than UTF-8. If you use that, See here https://www.php.net/manual/de/function.json-encode.php#115733 |
This comment has been minimized.
This comment has been minimized.
You could always create the getter for the object like this (implemented in a form of an universal trait)
And use it in as many objects as you like ( This way you keep your object immutable, read-only. |
This comment has been minimized.
This comment has been minimized.
The initial
will not work straight like this if your nested objects are custom, not |
This comment has been minimized.
This comment has been minimized.
just in case performance matters
from SO |
This comment has been minimized.
This comment has been minimized.
Absolutely brilliant. You, sir, are a genius. |
This comment has been minimized.
Do you know how many times I've had to do this over the past few years and it was ugly. This is definitely a life saver!