PHP - recursive Array to Object?

前端 未结 14 1269
夕颜
夕颜 2020-12-14 14:10

Is there a way to convert a multidimensional array to a stdClass object in PHP?

Casting as (object) doesn\'t seem to work recu

14条回答
  •  执笔经年
    2020-12-14 15:11

    You can use the array_map recursively:

    public static function _arrayToObject($array) {
        return is_array($array) ? (object) array_map([__CLASS__, __METHOD__], $array) : $array;
    }
    

    Works perfect for me since it doesn't cast for example Carbon objects to a basic stdClass (which the json encode/decode does)

提交回复
热议问题