irungoldstein
Goto Top

Mehrdimensionales Array mit PHP auslesen um es in eine MySql DB zu schreiben

Hallo Leute,

eventuell kann mir jemand hier auf die Sprünge helfen damit.

Ich habe ein Mehrdimensionales Array namens $tree

Es wird wie folgt befüllt:

<?PHP
$tree=array();

function getDirectory( $path = '.'){        
    
    $ignore = array( 'cgi-bin', '.', '..' );  
    
    $dh = @opendir( $path );

    $j=0;
    $temp=array();
        
    while( false !== ( $file = readdir( $dh ) ) ){
   
        if( !in_array( $file, $ignore ) ){
       
            $temp[$j]['name']=$file;  
            
            if( is_dir( "$path/$file" ) ){  

               $temp[$j]['children']=getDirectory( "$path/$file");  
            } 
       
        }
        
       $j++; 
    
    }
    return $temp;
   
    closedir( $dh );
    
}
$tree=getDirectory("/var/www/html/src/drive/");  

$type='desc';   

//recursive function for sorting arrays
function getSort(&$temp) {
 global $type;

 switch ($type) {
      case 'desc':  
      rsort($temp);
      break;
      case 'asc':  
      sort($temp);
      break;
}
 foreach($temp as &$t) {
   if(is_array($t['children']))  
                       getSort($t['children']);  
 }
} //end of function

getSort($tree);
?>

funktioniert auch alles wie gewünscht!

Das Ergebnis ist dann:
Array
(
     => Array
        (
            [name] => dir_name
            [children] => Array
                (
                     => Array
                        (
                            [name] => filename0.pdf
                        )

                    [1] => Array
                        (
                            [name] => filename1.pdf
                        )

                    [2] => Array
                        (
                            [name] => filename2.pdf
                        )

                    [3] => Array
                        (
                            [name] => filename3.pdf
                        )

                    [4] => Array
                        (
                            [name] => filename4.pdf
                        )

                    [5] => Array
                        (
                            [name] => filename5.pdf
                        )

                    [6] => Array
                        (
                            [name] => filename6.pdf
                        )

                    [7] => Array
                        (
                            [name] => filename7.pdf
                        )

                    [8] => Array
                        (
                            [name] => filename8.pdf
                        )

                )

        )
[1] => Array
        (
            [name] => dir_name2
            [children] => Array
                (
                     => Array
                        (
                            [name] => filenamexy.pdf
                        )

                    [1] => Array
                        (
                            [name] => filenamexyz.pdf
                        )

                    [2] => Array
                        (
                            [name] => filenamyejk.pdf
                        )

                    [3] => Array
                        (
                            [name] => kasdjfjkaskdljö.pdf
                        )

                )

        )

....... usw. 

Dieses Array würde ich gerne per PHP in eine MySql DB verarbeiten, ich steige aber gerade nicht durch wie ich es in folgende Form bekommen kann:

Spalte Ordner | Spalte Dateiname

sozusagen so lange [name] aus 1. Array, wie es Einträge mit [name] in 2. Array gibt.

Eventuell erbarmt sich ja jemand und hilft mir auf die Sprünge face-smile

Grüße

Content-Key: 314613

Url: https://administrator.de/contentid/314613

Ausgedruckt am: 19.03.2024 um 02:03 Uhr

Mitglied: 129813
Lösung 129813 07.09.2016 aktualisiert um 22:46:45 Uhr
Goto Top
Hi, you can do this with two nested foreach loops, the outer to iterate over your directory names and the inner loops over the children(the files) for each directory. Inside the inner loop you create the insert into statement for your database and you are finished.
Like this:
foreach ($tree as $dir){
   foreach($dir['children'] as $file){  
       // here you can access the file name with $file['name'] and the corresponding directory for the file with $dir['name'] 
      // create your insert into statement here
   }
}

Regards
Mitglied: IrunGoldstein
IrunGoldstein 08.09.2016 um 08:06:58 Uhr
Goto Top
Thank you very much,

it works perfectly!

Regards and have a nice day