Created
July 13, 2012 22:02
-
-
Save piercemoore/3107841 to your computer and use it in GitHub Desktop.
Convert a multi-dimensional array into an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Full disclosure: I found this solution here: http://www.richardcastera.com/blog/php-convert-array-to-object-with-stdclass and implemented it both here and in my project. | |
function arrayToObject( $array ) { | |
if ( !is_array( $array ) && !is_object( $array ) ) { | |
return $array; | |
} | |
$obj = new stdClass(); | |
if( ( is_array( $array ) || is_object( $array ) ) && count( $array ) > 0 ) { | |
foreach( $array as $k=>$v ) { | |
$obj->$k = arrayToObject( $v ); | |
} | |
return $obj; | |
} else { | |
return false; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment