Home >Backend Development >PHP Tutorial >Convert PHP object to array_PHP tutorial
Json or xml will be used to call data across platforms in the project. Need to convert json and xml objects into arrays. Just use the following function
/** * * 把对象转成数组 * @param $object 要转的对象$object */ function objectToArray($object){ $result = array(); $object = is_object($object) ? get_object_vars($object) : $object; foreach ($object as $key => $val) { $val = (is_object($val) || is_array($val)) ? objectToArray($val) : $val; $result[$key] = $val; } return $result; }