Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

php - get parent array name after array_walk_recursive function

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 ?

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

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 ?

Thank you for your help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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
";
        }
        else {
            // directory node -- recurse
            my_walk_recursive($v, $path.'/'.$k);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...