Home >Backend Development >PHP Tutorial >PHP custom function: recursively convert multi-dimensional array to one-dimensional array

PHP custom function: recursively convert multi-dimensional array to one-dimensional array

WBOY
WBOYOriginal
2016-07-25 09:04:391146browse
  1. function array_multi2single($array)

  2. {
  3. static $result_array=array();
  4. foreach($array as $value)
  5. {
  6. if(is_array($value))
  7. {
  8. array_multi2single($value);
  9. }
  10. else
  11. $result_array[]=$value;
  12. }
  13. return $result_array;
  14. }

  15. //测试

  16. $array=array("1"=>array("A","B","C",array("D","E")),"2"=>array("F","G","H","I"));
  17. $array=array_multi2single($array);
  18. echo "

    测试结果:

    ";
  19. foreach($array as $value)
  20. {
  21. echo "
    $value
    ";
  22. echo "
    ";
  23. }
  24. ?>

复制代码


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