How to convert an array to object in PHP?

前端 未结 30 2853
说谎
说谎 2020-11-22 02:48

How can I convert an array like this to an object?

[128] => Array
    (
        [status] => "Figure A.
 Facebook\'s horizontal scrollbars showing u         


        
30条回答
  •  再見小時候
    2020-11-22 03:11

    This requires PHP7 because I chose to use a lambda function to lock away the 'innerfunc' within the main function. The lambda function is called recursively, hence the need for: "use ( &$innerfunc )". You could do it in PHP5 but could not hide the innerfunc.

    function convertArray2Object($defs) {
        $innerfunc = function ($a) use ( &$innerfunc ) {
           return (is_array($a)) ? (object) array_map($innerfunc, $a) : $a; 
        };
        return (object) array_map($innerfunc, $defs);
    }
    

提交回复
热议问题