在array_walk_recursive函数之后获取父数组名称

use*_*571 7 php arrays multidimensional-array

我使用以下函数来验证搜索词是否在我的文件夹的文件名中.

$files2=list_files("documents/minelli");

Class Commentaire_filter{
      static function test_print($item, $key, $value)
      {
          if (preg_match("#".$value."#", $item))

          {
           $array =  Array($key=>$item);
          print_r($array);
          ?>

          <a href="documents/minelli/<?php echo $item; ?>"><?php echo $key.' '. $item; ?></a><br /> 
          <?php

          }
      }
}


array_walk_recursive($files2, 'Commentaire_filter::test_print',$motrecherche );
Run Code Online (Sandbox Code Playgroud)

我获取了一个文件列表.

我想添加一个允许用户下载文件的链接.

当我使用array_walk_recursive函数时,我只能获取文件名和密钥.如何获取父数组的名称来建立链接?

这是我的$文件的摘录:

数组(大小= 5)

'Administratifs'=>

array (size=5)

  0 => string 'campagne-sanmarina.jpg' (length=22)

  1 => string 'COSMO Echantillons ETE 2009.xls' (length=31)

  2 => string 'COSMOPARIS Echantillons MARO Hiver 2011 311-411.xls' (length=51)

  3 => string 'cosmoparis-boutique.jpg' (length=23)

  4 => string 'minelli-20-ans.png' (length=18)
Run Code Online (Sandbox Code Playgroud)

'Commerce'=>

array (size=4)

  0 => string 'a-gagner-cosmoparis.jpg' (length=23)

  1 => string 'CONTROLE 2009.pdf' (length=17)

  2 => string 'cosmoparis-boutique.jpg' (length=23)

  3 => string 'soldes-cosmoparis.jpg' (length=21)
Run Code Online (Sandbox Code Playgroud)

'Gestion'=>

array (size=1)

  'PROCEDURES' => 

    array (size=5)

      0 => string 'cosmoparis-boutique.jpg' (length=23)

      1 => string 'flux-ecommerce-smc.pdf' (length=22)

      2 => string 'Minelli-Lyon.jpg' (length=16)

      3 => string 'sanmarina-magasin-interieur.jpg' (length=31)

      4 => string 'visuel_chaussures_minelli_printemps_ete_2009.jpg' (length=48)
Run Code Online (Sandbox Code Playgroud)

'Magasins'=>

array (size=2)

  0 => string 'COSMO Echantillons ETE 2009.xls' (length=31)

  1 => string 'san-marina-saint-etienne.jpg' (length=28)
Run Code Online (Sandbox Code Playgroud)

'Ressources Humaines'=>

array (size=7)

  'ACTUALITES PAYE' => 

    array (size=3)

      0 => string 'COSMOPARIS Echantillons MARO Hiver 2011 311-411.xls' (length=51)

      1 => string 'cosmoparis-boutique25.jpg' (length=23)

      2 => string 'minelli-tours.jpg' (length=17)
          ...
Run Code Online (Sandbox Code Playgroud)

对于'cosmoparis-boutique25.jpg',我想获得父数组名称('Ressources Humaines'=>'ACTUALITES PAYE').我怎样才能获得这些信息来构建像'myfolder/Ressources Humaines/ACTUALITES PAYE/cosmoparis-boutique25.jpg这样的链接?

谢谢您的帮助!

Jon*_*Jon 7

你将不得不放弃array_walk_recursive自己的递归步行.这将允许您在递归时维护或传递自定义状态信息.

例如:

function my_walk_recursive(array $array, $path = null) {
    foreach ($array as $k => $v) {
        if (!is_array($v)) {
            // leaf node (file) -- print link
            $fullpath = $path.$v;
            // now do whatever you want with $fullpath, e.g.:
            echo "Link to $fullpath\n";
        }
        else {
            // directory node -- recurse
            my_walk_recursive($v, $path.'/'.$k);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 对于那些使用 Laravel &gt;=v4.2 的人来说, [array_dot()](https://laravel.com/docs/5.4/helpers#method-array-dot) 辅助函数非常方便地递归地展平嵌套数组,其中数组项目变为“parentKey.childKey =&gt; childValue”。 (2认同)