Home  >  Article  >  Backend Development  >  A little understanding of recursive traversal of PHP arrays

A little understanding of recursive traversal of PHP arrays

WBOY
WBOYOriginal
2016-07-25 09:04:51844browse
  1. /**
  2. desc: array
  3. */
  4. $arr = array (
  5. "1" => 'test',
  6. '2' => 'me',
  7. array (
  8. "3" => "beij",
  9. "4" => "zz"
  10. ),
  11. "45",
  12. array (
  13. "5" => "5",
  14. "6" => " ewrwer",
  15. "7" => "ssss",
  16. array (
  17. "8" => "ssd",
  18. "9" => "bdex"
  19. ),
  20. "10",
  21. "11"
  22. )
  23. );
  24. ?>
Copy code

Next, write a recursive function to print this array:

  1. /**
  2. desc: recursively traverse the array
  3. link: bbs.it-home.org
  4. date: 2013/2/22
  5. */
  6. function RecursiveArr($patten, $array) {
  7. foreach ( $array as $value ) {
  8. if (is_array ( $value )) {
  9. $patten = $patten . $patten;
  10. RecursiveArr ( $patten, $value );
  11. } else {
  12. echo "

    " . $patten . "【" . key ( $array ) . "】" . ": " . $value . "
    " . "

    ";
  13. next ( $array ); / / After assigning a value to the key, you need to move to the next one before the key will be updated. Call example:
RecursiveArr ( '-', $arr );
Copy the code

The result in the picture below appears:
As can be seen from the above running result diagram, some keys are found to be duplicated. In order to compare the differences and connections, let’s look at the code next:
/**
desc: recursively traverse the array

link: bbs.it-home.org A little understanding of recursive traversal of PHP arrays date: 2013/2/22

*/
function RecursiveArr($patten, $array) {
foreach ( $array as $value ) {
    if (is_array ( $value )) {
  1. $patten = $patten . $patten;
  2. RecursiveArr ( $patten, $value );
  3. } else {
  4. echo "

    " . $patten . "【" . key ( $array ) . "】" . ": " . $value . "
    " . "

    ";
  5. }
  6. next ( $array ) ; // After assigning a value to the key, you need to move to the next one before the key will be updated
  7. }
  8. }
  9. Copy code
  10. If I change the previous recursive function to the one above code, the running result diagram is as follows:
  11. You should be able to tell something from the two result pictures above, dear.
Instructions: If the next() function is used in the if-else code segment in foreach, the order of different levels can be recorded and followed; But if the next() function is used outside the if-else code segment in foreach, the order will increase directly without distinguishing levels. Therefore, if you need to process key values, you need to pay special attention to the changes here, otherwise it may make you fly like a bullet... Ha!

A little understanding of recursive traversal of PHP arrays

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