note 102735 deleted from language.types.object by googleguy
| From: | googleguy@php.net | Date: | Tue, 15 Jan 2013 04:45:23 +0000 |
| Subject: | note 102735 deleted from language.types.object by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193054@lists.php.net to get a copy of this message | ||
Note Submitter: contact at mullanaphy dot com
----
Have an stdClass and want to convert it recursively to an array, or vice versa? Here's two very
simple functions.
<?php
# A static class of mine, has more conversions in it, yet these two are the relevent ones.
final class Convert {
# Convert a stdClass to an Array.
static public function object_to_array(stdClass $Class){
# Typecast to (array) automatically converts stdClass -> array.
$Class = (array)$Class;
# Iterate through the former properties looking for any stdClass properties.
# Recursively apply (array).
foreach($Class as $key => $value){
if(is_object($value)&&get_class($value)==='stdClass'){
$Class[$key] = self::object_to_array($value);
}
}
return $Class;
}
# Convert an Array to stdClass.
static public function array_to_object(array $array){
# Iterate through our array looking for array values.
# If found recurvisely call itself.
foreach($array as $key => $value){
if(is_array($value)){
$array[$key] = self::array_to_object($value);
}
}
# Typecast to (object) will automatically convert array -> stdClass
return (object)$array;
}
}
?>