How to convert an array to object in PHP?

前端 未结 30 2685
说谎
说谎 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: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" } }

提交回复
热议问题