Warning: stream_socket_client(): php_network_getaddresses: getaddrinfo failed: System error in /home/mathieu/www/rodic-wp/wordpress-20160713/wp-includes/class-wp-http-streams.php on line 150

Warning: stream_socket_client(): unable to connect to ssl://rodic.fr:443 (php_network_getaddresses: getaddrinfo failed: System error) in /home/mathieu/www/rodic-wp/wordpress-20160713/wp-includes/class-wp-http-streams.php on line 150
Convert an array to an object in PHP – Mathieu Rodic
PHP_Logo

Convert an array to an object in PHP

What?

The function introduced below alows you to recursively convert an array to an object.

See also: Convert an object to an array in PHP

The code

function array_to_object($variable){
    if (is_array($variable)){
        $object = new StdClass();
        foreach ($variable as $key=>$value){
            $object->$key = array_to_object($value);
        }
        return $object;
    } else{
        return $variable;
    }
}

One thought on “Convert an array to an object in PHP

Leave a Comment


Warning: Unknown: open(/var/lib/php5/sessions/sess_l6vtabjim0fo80c9akc5vijqr1, O_RDWR) failed: Permission denied (13) in Unknown on line 0

Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php5/sessions) in Unknown on line 0