How to convert an array to object in PHP?

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

    Easy:

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

    Example:

    $array = array(
        'key' => array(
            'k' => 'value',
        ),
        'group' => array('a', 'b', 'c')
    );
    
    $object = json_decode(json_encode($array));
    

    Then, the following is true:

    $object->key->k === 'value';
    $object->group === array('a', 'b', 'c')
    

提交回复
热议问题