递归地走阵列和打印行走的路径 [英] Walk array recursively and print the path of the walk

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

问题描述

有人可以帮助我做一些code或说明书上怎么走递归数组,当到达最后一个元素打印到它的完整路径?一个简单的回声会工作,因为我将在code适应我正在开发一些其他的功能。

该功能并不需要计算数组维度,因为这将参数传递:

例如:

  $深度= 8;$阵列[1] [3] [5] [6] [9] [5] [8] [9];

在功能reachs第八元素它打印所有的路径设置为:

  //打印路径
1 - > 3 - > 5 - > 6 - > 9 - > 5 - > 8 - > 9'


  • 正如我所说,只有在这种幅面打印将工作原因我将实现code到一些其他的功能。


  • 数组键可具有相同的值。显然不是在整个arary相同的序列的相同的​​值。


更新:

步行递归功能:

  $的someArray [1] [2] [3] ='端';
$的someArray [1] [2] [6] =结束;
$的someArray [1] [3] [6] =结束;
$的someArray [4] [3] [7] =结束;功能li​​stArrayRecursive(安培; $ ARRAY_NAME,$ IDENT = 0){
    如果(is_array($ ARRAY_NAME)){
        的foreach($ ARRAY_NAME为$ K =>&安培; $ V){
            如果(is_array($ V)){
                为($ I = 0; $ I< $ IDENT * 10; $ I ++){回声&放大器; NBSP;; }
                回声$ķ。 :。 < BR>中;
                listArrayRecursive($ V,$ IDENT + 1);
            }其他{
                为($ I = 0; $ I< $ IDENT * 10; $ I ++){回声&放大器; NBSP;; }
                回声$ķ。 :。 $ V。 < BR>中;
            }
        }
    }其他{
        回声变量=。 $ ARRAY_NAME;
    }
}listArrayRecursive($的someArray);

将会打印:

  1:
      2:
                3:结束
                6:结束
      3:
                6:结束
4:
      3:
                7:结束

现在,我怎么能同时打印数组的路径每次到达终点?例如:

  1:
      2:
                3:结束:路径 - > 1,2,3
                6:结束:路径 - > 1,2,6
      3:
                6:结束:路径 - > 1,3,6
4:
      3:
                7:结束:路径 - > 4,3,7

EDITED code增加了第三个参数去记录路径:

  $的someArray [1] [2] [3] ='端';
$的someArray [1] [2] [6] =结束;
$的someArray [1] [3] [6] =结束;
$的someArray [4] [3] [7] =结束;
$的someArray [3] [2] =结束;功能li​​stArrayRecursive(安培; $ ARRAY_NAME,$ IDENT = 0,$ PATH = NULL){
     的foreach($ ARRAY_NAME为$ K =>&安培; $ V){
         如果(is_array($ V)){
            为($ I = 0; $ I< $ IDENT * 10; $ I ++){回声&放大器; NBSP;; }
            回声$ķ。 :。 < BR>中;
            $ PATH = $ķ。 ',';
            listArrayRecursive($ V,$ IDENT + 1,$路径);
        }其他{
             为($ I = 0; $ I< $ IDENT * 10; $ I ++){回声&放大器; NBSP;; }
             回声$ķ。 :。 $ V。 ' - 路径 - > 。 $路径。 < BR>中;
        }
    }
}listArrayRecursive($的someArray);

将会打印:

  1:
          2:
                    3:结束 - 路径 - > 1,2,
                    6:结束 - 路径 - > 1,2,
          3:
                    6:结束 - 路径 - > 1,2,3,
4:
          3:
                    7:结束 - 路径 - > 1,4,3,
3:
          2:结束 - 路径 - > 1,4,3,


解决方案

您可以采用一个 RecursiveIteratorIterator 文档)采取的辛勤工作通过阵列递归的。

 函数listArrayRecursive($的someArray){
    $ =迭代器新RecursiveIteratorIterator(新RecursiveArrayIterator($的someArray),RecursiveIteratorIterator :: SELF_FIRST);
    的foreach($迭代器为$ K => $ V){
        $缩进= str_repeat('和; NBSP;',10 * $ iterator-> getDepth());
        只显示关键://没有结束时
        如果(iterator- $> hasChildren()){
            回声$ $缩进K:< BR>中;
        //月末:显示键,值和路径
        }其他{
            为($ P =阵列(),$ I = 0,$ Z = $ iterator-> getDepth(); $ I< = $ž; $ I ++){
                $ P [] = $ iterator-> getSubIterator($ I) - GT;键();
            }
            $ PATH =破灭('',$ P);
            回声$ $缩进K:$ V:路径 - > $ PATH< BR>中;
        }
    }
}

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:

Example:

$depth = 8;

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

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.

Updated:

Walk recursively function:

$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);

Will print:

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);

Will print:

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, 

解决方案

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天全站免登陆