Get the name of the parent array after the array_walk_recursive function

I use the following function to check if there is a search word in the file name of my folder.

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

I get a list of files.

I would like to add a link to allow users to download the file.

When I use the array_walk_recursive function, I can only get the file name and key. How can I get the name of the parent arrays to make a link?

Here is an excerpt from my $ files:

array (size = 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) 

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

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

For 'cosmoparis-boutique25.jpg', I would like to get the name of the parent array ('Ressources Humaines' => 'ACTUALITES PAYE'). How can I get this information to create a link, for example, "myfolder / Ressources Humaines / ACTUALITES PAYE / cosmoparis-boutique25.jpg?

Thank you for your help!

+4
source share
1 answer

You will have to break through array_walk_recursive and scroll through your own recursive walk. This will allow you to store or transmit user state information when you recurs.

For instance:

 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, eg: echo "Link to $fullpath\n"; } else { // directory node -- recurse my_walk_recursive($v, $path.'/'.$k); } } } 
+5
source

Source: https://habr.com/ru/post/1498980/


All Articles