How to convert an array to object in PHP?

前端 未结 30 3354
说谎
说谎 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:25

    Best Method in the WORLD :)

    function arrayToObject($conArray)
    {
        if(is_array($conArray)){
            /*
            * Return array converted to object
            * Using __FUNCTION__ (Magic constant)
            * for recursive call
            */
            return (object) array_map(__FUNCTION__, $conArray);
        }else{
            // Return object
            return $conArray;
        }
    }
    

    if you use different methods you will have problems. This is the best method. You have ever seen.

提交回复
热议问题