Rekursionen und Return's

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Rekursionen und Return's

    Hi,
    hab ein kleines Problem mit Rekursionen und deren Return's.
    Bei folgendem Script:
    PHP Code:
        function getFiles($myDir="",$myFileSuffix=array("*"),$myDirDepth=0,$tmp_depth=0)
        {   
            
    // Reguläre Variablen:
            
    $dir=$myDir;
            
    $suffix=$myFileSuffix;
            
    $files=glob($dir."*");
            
    $depth=$myDirDepth;
            
    $container[]=(empty($container)) ? array() : $container;
            
            
    // Verzeichnisse/Dateien auslesen:
            
    foreach ($files as $counter1 => $file)
            {            
    $basename=basename($file);
                
    $thisSuffix=substr($basename,strlen($basename)-3);
                
                foreach (
    $suffix as $counter2 => $value)
                {                                           
                    if(!
    is_dir($file) && ($thisSuffix == $value || $suffix[0] == "*"))
                    {
                        
    //echo $file." :: ".""."<br />";
                        
    $container[$counter1] .= $file;
                    }
                    else if(
    $tmp_depth<$depth-|| $depth == 0)
                    {
                        
    getFiles($file."/",$myFileSuffix,$myDirDepth,$tmp_depth+1);
                    }
                }
            }
            
            
    array_multisort($container);
            
    //print_r($container);
            
    return($container);
        }
        
        
    // Funktion aufrufen:
        
    print_r(getFiles("dir0/",array("*"),2)); 
    Mein Problem ist nun, dass eigentlich mehrere arrays
    zurückgegeben werden müssen, allerdings durch die
    Rekursion krieg ich nur ein array mit einem return zurück,
    kann ich das Problem umgehen? Ich bräucht eben alle
    arrays in einem array (also 2 Dimensionales array) als
    Return, was aber ja nicht klappt da die Arrays in
    verschiedenen Rekursionen existieren.

    Gruß darki

  • #2
    Hm ... static bei der Deklaration ist dein Freund

    Comment


    • #3
      Static, ist evtl. etwas unglücklich, weil es dann bei jedem Aufruf der Funktion wieder geleert werden muß, sonst produziert es Müll. Eine Sonderbehandlung zuviel

      evtl. so:
      PHP Code:
       function all_files($dir
      {
          
      $result=array();
          
      $dieser_ordner glob($dir.'/*');
          if(
      is_array($dieser_ordner))
          {
            foreach(
      $dieser_ordner as $value)
            {
              if(
      is_file($value)) $result[] = $value;
              if(
      is_dir($value))
                
      $result array_merge($result,all_files($value));
            }
          }
          return 
      $result;

      print_r(all_files('.')); 
      *edit*
      Kommando zurück...!!
      Ich sehe gerade, dass auch die Tiefe übergeben werden soll. Da ist natürlich static das richtige Mittel!!
      (wer lesen kann, ist klar im vorteil)

      Also ignoriert meinen Beitrag bitte....
      Last edited by combie; 18-08-2007, 20:50.
      Wir werden alle sterben

      Comment


      • #4
        Habs nun anderweitig gelöst, da er mir zuviel verwurschtelt hat,
        da es letzten Endes 3-dimensionale Arrays wurden.
        Hab die Struktur der Arrays nun folgendermassen aufgebaut:
        PHP Code:
        Array
        (
            [
        0] => Array
                (
                    [
        file] => e.txt
                    
        [dir_depth] => 3
                
        )

            [
        1] => Array
                (
                    [
        file] => a.txt
                    
        [dir_depth] => 1
                
        )

            [
        2] => Array
                (
                    [
        file] => h.txt
                    
        [dir_depth] => 2
                
        )

            [
        3] => Array
                (
                    [
        file] => g.txt
                    
        [dir_depth] => 1
                
        )

            [
        4] => Array
                (
                    [
        file] => k.txt
                    
        [dir_depth] => 2
                
        )

            [
        5] => Array
                (
                    [
        file] => f.txt
                    
        [dir_depth] => 3
                
        )


        Leider klappt meine Sortierfunktion noch nicht so ganz wie ich
        es möchte, man nehme einmal dieses Beispiel:
        PHP Code:
            $new 
            array(
            array(
        "file" => "e.txt""dir_depth" => 3),
            array(
        "file" => "a.txt""dir_depth" => 1),
            array(
        "file" => "h.txt""dir_depth" => 2),
            array(
        "file" => "g.txt""dir_depth" => 1),
            array(
        "file" => "k.txt""dir_depth" => 2),
            array(
        "file" => "f.txt""dir_depth" => 3));
            

            function 
        cmp($a$b
            {
                
        $c strcmp($a["dir_depth"], $b["dir_depth"]);
                
        $d strcmp($a["file"], $b["file"]);

                return 
        $c;
            }

            
        usort($new"cmp");
            
        print_r($new); 
        Nun möchte ich, dass er nach "dir_depth" sowie "file"
        zugleich sortiert, also beide in Abhängigkeit voneinander.
        Leider weis ich nicht wie ich die beiden miteinander koppeln
        soll. Im moment schaff ich es nur soweit dass er mir entweder
        nur nach "dir_depth" ($c) sortiert oder nur nach "file" ($d).
        Wie könnte man das machen?

        So sollte es zum Schluss aussehen:
        PHP Code:
        Array
        (
            [
        0] => Array
                (
                    [
        file] => a.txt
                    
        [dir_depth] => 1
                
        )
                
            [
        1] => Array
                (
                    [
        file] => g.txt
                    
        [dir_depth] => 1
                
        )
                
            [
        2] => Array
                (
                    [
        file] => h.txt
                    
        [dir_depth] => 2
                
        )

            [
        3] => Array
                (
                    [
        file] => k.txt
                    
        [dir_depth] => 2
                
        )

            [
        4] => Array
                (
                    [
        file] => e.txt
                    
        [dir_depth] => 3
                
        )
                
            [
        5] => Array
                (
                    [
        file] => f.txt
                    
        [dir_depth] => 3
                
        )

        Gruß darki

        Comment


        • #5
          Code:
          function cmp(a, b) {
          if (a.depth != b.depth)
             return (a.depth < b.depth ? -1 : 1);
          else
             return strcmp(a.name, b.name)
          
          }
          oder so ähnlich

          Comment


          • #6
            Großen Dank euch allen, hier nochmal die Zusammenfassung für das Sortierungsbeispiel:
            PHP Code:
            <?php
                $new 

                array(
                array(
            "file" => "e.txt""dir_depth" => 3),
                array(
            "file" => "a.txt""dir_depth" => 1),
                array(
            "file" => "h.txt""dir_depth" => 2),
                array(
            "file" => "g.txt""dir_depth" => 1),
                array(
            "file" => "k.txt""dir_depth" => 2),
                array(
            "file" => "f.txt""dir_depth" => 3));
                

                function 
            cmp($a$b
                {
                    if (
            $a["dir_depth"] != $b["dir_depth"])
                       return (
            $a["dir_depth"] < $b["dir_depth"] ? -1);
                    else
                       return 
            strcmp($a["file"], $b["file"]);
                } 

                
            usort($new"cmp");
                
            print_r($new);
            ?>
            Gruß darki
            Last edited by darki777; 19-08-2007, 14:42.

            Comment


            • #7
              meiner Meinung nach ist static auch nicht gut!

              ich würde einfach eine Referenz mitlaufen lassen!
              dann hast du das "return" Problem auch nicht!

              Einfach in der Funktion hinten: "& $data" und fertig!
              Find Parties?
              Partysuche

              Comment

              Working...
              X