Home > Backend Development > PHP Tutorial > Simple example code for PHP to recursively print all elements in an array

Simple example code for PHP to recursively print all elements in an array

WBOY
Release: 2016-07-25 08:58:09
Original
1234 people have browsed it
This article introduces an example of recursively printing all elements in an array in PHP. Friends in need can refer to it.

In PHP programming, use recursive algorithm to print out all elements in the array. The custom function is as follows:

<?php
/**
* 递归打印数组中所有元素
* edit bbs.it-home.org
*/
    //@patten:为打印前的字符串,不同的层次会增加该串打印的次数
    //@array:为要打印的数组
    function print_array($patten, $array){
      $retstr;
      foreach($array as $value){
        if( is_array($value) ){
          $patten = $patten.$patten;
          print_array($patten, $value);
        } else {
          echo "<p>".$patten."[".key($array)."]".": ".$value." <br/> ". "</p>";
        }
      next($array);
      }
    }
?>
Copy after login

Calling method:

print_array("-", $array);
Copy after login


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template