How to convert an array to object in PHP?

前端 未结 30 2842
说谎
说谎 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);
    }
    
    0 讨论(0)
  • 2020-11-22 03:12

    You can use the (object) function to convert your array into an object.

    $arr= [128=> ['status'=>
                     'Figure A. Facebook \'s horizontal scrollbars showing up on a 1024x768 screen resolution.'],
                      129=>['status'=>'The other day at work, I had some spare time']];
    
                $ArrToObject=(object)$arr;
                var_dump($ArrToObject);
    

    The result will be an object that contains arrays:

    object(stdClass)#1048 (2) { [128]=> array(1) {

    ["status"]=> string(87) "Figure A. Facebook 's horizontal scrollbars showing up on a 1024x768 screen resolution." }

    [129]=> array(1) { ["status"]=> string(44) "The other day at work, I had some spare time" } }

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-11-22 03:13

    You could also do this by adding (object) on left of variable to create a new object.

    <?php
    $a = Array
        ( 'status' => " text" );
    var_dump($a);
    $b = (object)$a;
    var_dump($b);
    var_dump($b->status);
    

    http://codepad.org/9YmD1KsU

    0 讨论(0)
  • 2020-11-22 03:13

    Inspired by all these codes, i tried to create a enhanced version with support to: specific class name, avoid constructor method, 'beans' pattern and strict mode (set only existing properties):

        class Util {
    
    static function arrayToObject($array, $class = 'stdClass', $strict = false) {
            if (!is_array($array)) {
                return $array;
            }
    
            //create an instance of an class without calling class's constructor
            $object = unserialize(
                    sprintf(
                            'O:%d:"%s":0:{}', strlen($class), $class
                    )
            );
    
            if (is_array($array) && count($array) > 0) {
                foreach ($array as $name => $value) {
                    $name = strtolower(trim($name));
                    if (!empty($name)) {
    
                        if(method_exists($object, 'set'.$name)){
                            $object->{'set'.$name}(Util::arrayToObject($value));
                        }else{
                            if(($strict)){
    
                                if(property_exists($class, $name)){
    
                                    $object->$name = Util::arrayToObject($value); 
    
                                }
    
                            }else{
                                $object->$name = Util::arrayToObject($value); 
                            }
    
                        }
    
                    }
                }
                return $object;
            } else {
                return FALSE;
            }
            }
    }
    
    0 讨论(0)
  • 2020-11-22 03:14

    you can simply use type casting to convert an array to object.

    // *convert array to object* Array([id]=> 321313[username]=>shahbaz)
    $object = (object) $array_name;
    
    //now it is converted to object and you can access it.
    echo $object->username;
    
    0 讨论(0)
提交回复
热议问题