This article describes the use of recursive algorithms to infinitely traverse arrays. Share it for your reference, as follows:
(PS: For easy reading, the code here uses the php code formatting toolhttp://tools./code/phpformatFormatted)
<?php //Infinite traversal of the array$a1 = array("a", "b", "c"); //One-dimensional array$a2 = array(array(21, 3, 6), array("a", "b", "c")); //Two-dimensional array$a3 = array(array(array(5, 55), 4, 444), 2, 7, 6, 8, array("w", "d", array(3, 2, "a"), "s")); //Multi-dimensional irregular arrayfunction fun($a) { foreach ($a as $val) { if (is_array($val)) { //If the key value is an array, recursive call of the function is performed fun($val); } else { // If the key value is a numeric value, output it echo "$val<br />"; } //end if } //end foreach } //end fun //fun($a1); //fun($a2); fun($a3); ?>
Output:
5 55 4 444 2 7 6 8 w d 3 2 a s
For more information about PHP related content, please check out the topic of this site:PHP data structure and algorithm tutorial》、《Summary of PHP Programming Algorithm》、《Summary of php sorting algorithm》、《Summary of common traversal algorithms and techniques for PHP》、《Summary of PHP mathematical operation skills》、《Complete collection of PHP array (Array) operation techniques》、《Summary of usage of php strings"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.