2010-06-29 37 views
0

如果,例如,我有數組是這樣的:瞭解析S3數據陣列

array(34) { 
    ["ahostel.lt/img/background.png"]=> 
    array(4) { 
    ["name"]=> 
    string(29) "ahostel.lt/img/background.png" 
    ["time"]=> 
    int(1277819688) 
    ["size"]=> 
    int(36811) 
    ["hash"]=> 
    string(32) "2600e98e10aba543fb2637b701dec4f3" 
    } 
    ["ahostel.lt/img/body-navigation-bg.png"]=> 
    array(4) { 
    ["name"]=> 
    string(37) "ahostel.lt/img/body-navigation-bg.png" 
    ["time"]=> 
    int(1277819688) 
    ["size"]=> 
    int(409) 
    ["hash"]=> 
    string(32) "2e8743f24d46748c5919dfa44b51c2a5" 
    } 
    ["ahostel.lt/img/calendar-input.gif"]=> 
    array(4) { 
    ["name"]=> 
    string(33) "ahostel.lt/img/calendar-input.gif" 
    ["time"]=> 
    int(1277819688) 
    ["size"]=> 
    int(630) 
    ["hash"]=> 
    string(32) "45d5148e7937fc75a530d7ceb73b7bc8" 
    } 
    ["ahostel.lt/img/facebook-icon.png"]=> 
    array(4) { 
    ["name"]=> 
    string(32) "ahostel.lt/img/facebook-icon.png" 
    ["time"]=> 
    int(1277819688) 
    ["size"]=> 
    int(1090) 
    ["hash"]=> 
    string(32) "8a76e313b097f53d0f225a5db6f9ae6b" 
    } 
    ["ahostel.lt/img/favicon.png"]=> 
    array(4) { 
    ["name"]=> 
    string(26) "ahostel.lt/img/favicon.png" 
    ["time"]=> 
    int(1277819689) 
    ["size"]=> 
    int(505) 
    ["hash"]=> 
    string(32) "daa5091eb2c059fc36b54d521e589a50" 
    } 
[...] 

什麼是從路徑來獲取所有的目錄和文件的最佳sollution(在不同的陣列,雖然) ahostel.lt/img/。我現在有很多foreach循環,這看起來不像是一個好的解決方案。

+0

你能澄清嗎?你想提取文件名,還是想分割路徑來獲取目錄,或者你想檢索實際的文件? – 2010-06-29 15:31:34

+0

但是,我想獲得一個這樣的數組,只有* ahostel.lt/img/*路徑中的文件信息。 – Gajus 2010-06-29 15:34:41

回答

0
$files   = array(); 
$directories = array(); 

foreach($this->file_system as $key => $file_object_info) 
{ 
    // make sure this is the requested path and exclude it from appearaning twice 
    if(strpos($key, $path) === 0 && $key !== $path) 
    { 
     $relative_path = mb_substr($key, mb_strlen($path)); 

     // this is a file 
     if(strpos($relative_path, '/') === FALSE) 
     { 
      $files[$key]  = $file_object_info; 
     } 
     // this is a directory 
     elseif(!substr($relative_path, strpos($relative_path, '/') + 1)) 
     { 
      $directories[$key] = $file_object_info; 
     } 
    } 
} 

我認爲這是最好的,我可以優化它。雖然它不會像我一樣糟糕。