Last Updated: February 25, 2016
·
7.213K
· Wahyu Kristianto

The Fastest Way to Convert Array to Object (multidimensional)

$object  = (object)json_decode(json_encode($data));
$array   = json_decode(json_encode($data), TRUE);

$data= array(
    'base_url'          => 'http://example.com/',
    'uri_protocol'      => 'AUTO',
    'url_suffix'        => '',
    'language'          => 'english',
    'modules_locations' => array(
        'core_module'   => 'packages/core_modules/',
        'module'        => 'packages/modules/'
    )
);

$convert = json_encode($data);
$object  = (object)json_decode($convert);
$array   = json_decode($convert, TRUE);

echo "<pre>";
print_r($object);
print_r($array);
echo "</pre>";

4 Responses
Add your response

What about (object) array();?

over 1 year ago ·

@robinvdvleuten (object)array(); does not support multidimensional array

over 1 year ago ·

Ah good to know, thanks

over 1 year ago ·

Is this really the "fastest" way or just the most convenient? :D

over 1 year ago ·