Home >Backend Development >PHP Tutorial >PHP function to convert object to array (recursive method)

PHP function to convert object to array (recursive method)

WBOY
WBOYOriginal
2016-07-25 09:03:211029browse
  1. function object_to_array($obj)
  2. {
  3. $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
  4. foreach ($_arr as $key => $val)
  5. {
  6. $val = (is_array($val) || is_object($val)) ? object_to_array($val) : $val;
  7. $arr[$key] = $val;
  8. }
  9. return $arr;
  10. }
  11. ?>
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn