Skip to content

@piercemoore /arrayToObject.php
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Convert a multi-dimensional array into an object
<?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
Something went wrong with that request. Please try again.