PHP - recursive Array to Object?

2019-01-23 01:11发布

Is there a way to convert a multidimensional array to a stdClass object in PHP?

Casting as (object) doesn't seem to work recursively. json_decode(json_encode($array)) produces the result I'm looking for, but there has to be a better way...

14条回答
We Are One
2楼-- · 2019-01-23 01:54

I know this answer is coming late but I'll post it for anyone who's looking for a solution.

Instead of all this looping etc, you can use PHP's native json_* function. I've got a couple of handy functions that I use a lot

/**
 * Convert an array into a stdClass()
 * 
 * @param   array   $array  The array we want to convert
 * 
 * @return  object
 */
function arrayToObject($array)
{
    // First we convert the array to a json string
    $json = json_encode($array);

    // The we convert the json string to a stdClass()
    $object = json_decode($json);

    return $object;
}


/**
 * Convert a object to an array
 * 
 * @param   object  $object The object we want to convert
 * 
 * @return  array
 */
function objectToArray($object)
{
    // First we convert the object into a json string
    $json = json_encode($object);

    // Then we convert the json string to an array
    $array = json_decode($json, true);

    return $array;
}

Hope this can be helpful

查看更多
唯我独甜
3楼-- · 2019-01-23 01:58
/**
 * Recursively converts associative arrays to stdClass while keeping integer keys subarrays as arrays
 * (lists of scalar values or collection of objects).
 */
function a2o( array $array ) {
    $resultObj = new \stdClass;
    $resultArr = array();
    $hasIntKeys = false;
    $hasStrKeys = false;
    foreach ( $array as $k => $v ) {
        if ( !$hasIntKeys ) {
            $hasIntKeys = is_int( $k );
        }
        if ( !$hasStrKeys ) {
            $hasStrKeys = is_string( $k );
        }
        if ( $hasIntKeys && $hasStrKeys ) {
            $e = new \Exception( 'Current level has both integer and string keys, thus it is impossible to keep array or convert to object' );
            $e->vars = array( 'level' => $array );
            throw $e;
        }
        if ( $hasStrKeys ) {
            $resultObj->{$k} = is_array( $v ) ? a2o( $v ) : $v;
        } else {
            $resultArr[$k] = is_array( $v ) ? a2o( $v ) : $v;
        }
    }
    return ($hasStrKeys) ? $resultObj : $resultArr;
} 
查看更多
我只想做你的唯一
4楼-- · 2019-01-23 01:58

Late, but just wanted to mention that you can use the JSON encoding/decoding to convert fully from/to array:

//convert object $object into array
$array = json_decode(json_encode($object), true);
//convert array $array into object
$object = json_decode(json_encode($array));

json_encode and json_decode functions are available starting from php 5.2

查看更多
SAY GOODBYE
5楼-- · 2019-01-23 01:59

You and many others have pointed to the JSON built-in functions, json_decode() and json_encode(). The method which you have mentioned works, but not completely: it won't convert indexed arrays to objects, and they will remain as indexed arrays. However, there is a trick to overcome this problem. You can use JSON_FORCE_OBJECT constant:

// Converts an array to an object recursively
$object = json_decode(json_encode($array, JSON_FORCE_OBJECT));

Tip: Also, as mentioned here, you can convert an object to array recursively using JSON functions:

// Converts an object to an array recursively
$array = json_decode(json_encode($object), true));    
查看更多
家丑人穷心不美
6楼-- · 2019-01-23 02:01
function toObject($array) {
    $obj = new stdClass();
    foreach ($array as $key => $val) {
        $obj->$key = is_array($val) ? toObject($val) : $val;
    }
    return $obj;
}
查看更多
Viruses.
7楼-- · 2019-01-23 02:02

The simpliest way to convert an associative array to object is:

First encode it in json, then decode it.

like $objectArray = json_decode(json_encode($associtiveArray));

查看更多
登录 后发表回答