Home >Backend Development >PHP Tutorial >PHP multi-dimensional array to string and multi-dimensional array to one-dimensional array code

PHP multi-dimensional array to string and multi-dimensional array to one-dimensional array code

WBOY
WBOYOriginal
2016-07-25 09:04:38892browse
  1. /**

  2. * @method Multidimensional array to string
  3. * @staticvar array $result_array
  4. * @param type $array
  5. * @return type
  6. * @link bbs.it-home.org
  7. */
  8. function arrayToString($arr)
  9. {
  10. if (is_array($arr))
  11. {
  12. return implode(',', array_map('arrayToString', $arr));
  13. }
  14. return $arr;
  15. }

  16. /**

  17. * @method Multidimensional array becomes one-dimensional array
  18. * @staticvar array $result_array
  19. * @param type $array
  20. * @return type
  21. * @link bbs.it-home.org
  22. */
  23. function multi2array($array) {
  24. static $result_array = array();
  25. foreach ($array as $key => $value) {
  26. if (is_array($value)) {
  27. array_multi2array($value);
  28. }
  29. else
  30. $result_array[$key] = $value;
  31. }
  32. return $result_array;
  33. }

复制代码


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