2013-08-26 23 views
4

我使用以下函数来验证搜索词是否在我的文件夹的文件名中。在array_walk_recursive函数后获取父数组名称

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

我获得了一个文件列表。

我想添加一个链接,以允许用户下载文件。

当我使用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) 

'商业'=>

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) 

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

'Magasins百货公司'=>

array (size=2) 

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

    1 => string 'san-marina-saint-etienne.jpg' (length=28) 

'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) 
      ... 

对于 'cosmoparis-boutique25.jpg',我想获取父阵列名称('Ressources Humaines'=>'ACTUALITES PAYE')。我如何获得这些信息来建立一个链接,如'myfolder/Ressources Humaines/ACTUALITES PAYE/cosmoparis-boutique25.jpg?

谢谢你的帮助!

回答

5

你将不得不沟通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); 
     } 
    } 
} 
+0

非常感谢你为你的脚本,它是完美!!!! – user2718571

+0

悲伤...这需要实施,我发现,即使回调传递的数组键是整数键,而不是关联 - 像... –

+0

对于那些使用Laravel> = v4.2 [array_dot()](https ://laravel.com/docs/5.4/helpers#method-array-dot)在数组项变为'parentKey.childKey => childValue'的情况下,helper函数非常方便地递归地平滑嵌套数组。 – alexkb