How to convert an array to object in PHP?

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

    use this function that i've made:

    function buildObject($class,$data){
        $object = new $class;
        foreach($data as $key=>$value){
            if(property_exists($class,$key)){
                $object->{'set'.ucfirst($key)}($value);
            }
        }
        return $object;
    }
    

    Usage:

    $myObject = buildObject('MyClassName',$myArray);
    

提交回复
热议问题