How to convert an array to object in PHP?

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

    Actually if you want to use this with multi-dimensional arrays you would want to use some recursion.

    static public function array_to_object(array $array)
    {
        foreach($array as $key => $value)
        {
            if(is_array($value))
            {
                $array[$key] = self::array_to_object($value);
            }
        }
        return (object)$array;
    }
    

提交回复
热议问题