I use the following function to verify if a search word is in the name of files 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 obtain a list of files.
我获取了一个文件列表。
I would like add a link to permit users to download the file.
我想添加一个允许用户下载文件的链接。
When i'm using the array_walk_recursive function, i can only get the name of file and the key. How can i get the name of parent arrays to made the link ?
当我使用array_walk_recursive函数时,我只能获取文件名和密钥。如何获取父数组的名称来建立链接?
Here an extract of 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' =>
'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 parent array name ('Ressources Humaines'=>'ACTUALITES PAYE'). How can i get this information to build a link like 'myfolder/Ressources Humaines/ACTUALITES PAYE/cosmoparis-boutique25.jpg ?
对于'cosmoparis-boutique25.jpg',我想得到父数组名称('Ressources Humaines'=>'ACTUALITES PAYE')。我怎样才能获得这些信息来构建像'myfolder / Ressources Humaines / ACTUALITES PAYE / cosmoparis-boutique25.jpg这样的链接?
Thank you for your help!
谢谢您的帮助!
1 个解决方案
#1
6
You will have to ditch array_walk_recursive
and roll your own recursive walk. This will allow you to maintain or pass custom state information whenever you recurse.
您将不得不抛弃array_walk_recursive并滚动您自己的递归步行。这将允许您在递归时维护或传递自定义状态信息。
For example:
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);
}
}
}
#1
6
You will have to ditch array_walk_recursive
and roll your own recursive walk. This will allow you to maintain or pass custom state information whenever you recurse.
您将不得不抛弃array_walk_recursive并滚动您自己的递归步行。这将允许您在递归时维护或传递自定义状态信息。
For example:
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);
}
}
}