递归地走数组并打印走的路径 [英] Walk array recursively and print the path of the walk

查看:17
本文介绍了递归地走数组并打印走的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我提供一些关于如何递归遍历数组以及在到达最后一个元素时打印完整路径的代码或说明吗?一个简单的 echo 会起作用,因为我会将代码调整为我正在开发的其他一些功能.

Can someone help me with some code or instructions on how to walk recursively an array and when reaching the last element print the full path to it? A simple echo will work because I will adapt the code to some other function I'm developing.

函数不需要计算数组维度,因为这个参数会被传递:

The function doesn't need to figure the array dimension because this param will be passed:

示例:

$depth = 8;

$array[1][3][5][6][9][5][8][9];

当函数到达第 8 个元素时,它会打印到它的所有路径:

When function reachs the 8th element it print all the path to it:

//print path
'1 -> 3 -> 5 -> 6 -> 9 -> 5 -> 8 -> 9'

  • 正如我所说,只有以这种格式打印才能工作,因为我会将代码实现到其他一些功能中.

    • As I said, only printing in this format will work cause I will implement the code into some other function.

      数组键可以具有相同的值.对于整个数组,显然不是相同序列中的相同值.

      array keys can have the same value. Obviously not the same value in the same sequence for the entire arary.

      更新:

      递归遍历函数:

      $someArray[1][2][3] = 'end';
      $someArray[1][2][6] = 'end';
      $someArray[1][3][6] = 'end';
      $someArray[4][3][7] = 'end';
      
      function listArrayRecursive(&$array_name, $ident = 0){
          if (is_array($array_name)){
              foreach ($array_name as $k => &$v){
                  if (is_array($v)){
                      for ($i=0; $i < $ident * 10; $i++){ echo "&nbsp;"; }
                      echo $k . " : " . "<br>";
                      listArrayRecursive($v, $ident + 1);
                  }else{
                      for ($i=0; $i < $ident * 10; $i++){ echo "&nbsp;"; }
                      echo $k . " : " . $v . "<br>";
                  }
              }
          }else{
              echo "Variable = " . $array_name;
          }
      }
      
      listArrayRecursive($someArray);
      

      将打印:

      1 :
            2 :
                      3 : end
                      6 : end
            3 :
                      6 : end
      4 :
            3 :
                      7 : end
      

      现在,我怎样才能在每次到达末尾时打印数组的路径?例如:

      Now, how can I also print the path of the array everytime it reaches the end? For example:

      1 :
            2 :
                      3 : end : path -> 1,2,3
                      6 : end : path -> 1,2,6
            3 :
                      6 : end : path -> 1,3,6
      4 :
            3 :
                      7 : end : path -> 4,3,7
      

      编辑代码添加第三个参数来记录路径:

      EDITED CODE ADDING A THIRD PARAM TO RECORD THE PATH:

      $someArray[1][2][3] = 'end';
      $someArray[1][2][6] = 'end';
      $someArray[1][3][6] = 'end';
      $someArray[4][3][7] = 'end';
      $someArray[3][2] = 'end';
      
      function listArrayRecursive(&$array_name, $ident = 0, $path = null){
           foreach ($array_name as $k => &$v){
               if (is_array($v)){
                  for ($i=0; $i < $ident * 10; $i++){ echo "&nbsp;"; }
                  echo $k . " : " . "<br>";
                  $path .= $k . ', ';
                  listArrayRecursive($v, $ident + 1, $path);
              }else{
                   for ($i=0; $i < $ident * 10; $i++){ echo "&nbsp;"; }
                   echo $k . " : " . $v . ' - path -> ' . $path . "<br>";
              }
          }
      }
      
      listArrayRecursive($someArray);
      

      将打印:

      1 :
                2 :
                          3 : end - path -> 1, 2,
                          6 : end - path -> 1, 2,
                3 :
                          6 : end - path -> 1, 2, 3,
      4 :
                3 :
                          7 : end - path -> 1, 4, 3,
      3 :
                2 : end - path -> 1, 4, 3, 
      

      推荐答案

      您可以使用 RecursiveIteratorIterator (docs) 以减少遍历数组的繁重工作.

      You could employ a RecursiveIteratorIterator (docs) to take the hard work out of recursing through the arrays.

      function listArrayRecursive($someArray) {
          $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($someArray), RecursiveIteratorIterator::SELF_FIRST);
          foreach ($iterator as $k => $v) {
              $indent = str_repeat('&nbsp;', 10 * $iterator->getDepth());
              // Not at end: show key only
              if ($iterator->hasChildren()) {
                  echo "$indent$k :<br>";
              // At end: show key, value and path
              } else {
                  for ($p = array(), $i = 0, $z = $iterator->getDepth(); $i <= $z; $i++) {
                      $p[] = $iterator->getSubIterator($i)->key();
                  }
                  $path = implode(',', $p);
                  echo "$indent$k : $v : path -> $path<br>";
              }
          }
      }
      

      这篇关于递归地走数组并打印走的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆