How to convert an array to object in PHP?

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

    I also had this issue, but I noticed that json_decode converts JSON array to object.

    So, I came about my solution by using json_encode($PHPArray) which returns A JSON string of object, then I decoded the string with Json_decode($string) and it would return a perfectly structured object. Shorthand

    $object = json_decode(json_encode($array));
    

    Or

    $jsonString = json_encode($array);
    $object = json_decode($jsonString);
    

提交回复
热议问题