Convert a PHP object to an associative array

后端 未结 30 2009
走了就别回头了
走了就别回头了 2020-11-22 02:18

I\'m integrating an API to my website which works with data stored in objects while my code is written using arrays.

I\'d like a quick-and-dirty function to convert

30条回答
  •  离开以前
    2020-11-22 02:31

    Some impovements to the "well-knwon" code

    /*** mixed Obj2Array(mixed Obj)***************************************/ 
    static public function Obj2Array($_Obj) {
        if (is_object($_Obj))
            $_Obj = get_object_vars($_Obj);
        return(is_array($_Obj) ? array_map(__METHOD__, $_Obj) : $_Obj);   
    } // BW_Conv::Obj2Array
    

    Notice that if the function is member of a class (like above) you must change __FUNCTION__ to __METHOD__

提交回复
热议问题