2013-07-23 161 views
3

多維數組我現在有這樣的代碼:PHP - 分組通過鍵值

$files = glob('songs/*.{mp3}', GLOB_BRACE); 
$dateUploaded = array(); 

foreach($files as $file){ 
    $path_parts = pathinfo($file); 
    $dateUploaded[] = array("Date Uploaded" => date ("jS \of F, Y", filemtime($file)), "File Name" => $path_parts['filename']); 
} 
print_r($dateUploaded); 

,輸出

Array (
    [0] => Array (
     [Date Uploaded] => 26th of February, 2013 
     [File Name] => All That Glitters - This Sound (Felix Cartal Remix) 
    ) 
    [1] => Array (
     [Date Uploaded] => 26th of February, 2013 
     [File Name] => Bertie Blackman - Hide and Seek 
    ) 
    [2] => Array (
     [Date Uploaded] => 18th of December, 2012 
     [File Name] => Birds of Tokyo - This Fire 
    ) 
    [3] => Array (
     [Date Uploaded] => 18th of December, 2012 
     [File Name] => Bloc Party - Truth 
    ) 
    [4] => Array (
     [Date Uploaded] => 18th of December, 2012 
     [File Name] => C2C - Down the Road 
    ) 
    [5] => Array (
     [Date Uploaded] => 18th of December, 2012 
     [File Name] => Chance Waters - Young and Dumb feat Bertie Blackman 
    ) 
    [6] => Array (
     [Date Uploaded] => 26th of February, 2013 
     [File Name] => City Calm Down - Sense of Self 
    ) 
    [7] => Array (
     [Date Uploaded] => 18th of December, 2012 
     [File Name] => Clubfeet - Heartbreak 
    ) 
    [8] => Array (
     [Date Uploaded] => 26th of February, 2013 
     [File Name] => Cosmo Jarvis - My Own Thing 
    ) 
    [9] => Array (
     [Date Uploaded] => 6th of June, 2013 
     [File Name] => Courtney Barnett - History Eraser 
    ) 
    [10] => Array (
     [Date Uploaded] => 6th of June, 2013 
     [File Name] => Delphic - Baiya 
    ) 
) 

什麼,我試圖做到這一點組,這些由「上傳日期」鍵。所以我有類似這樣的東西:

Array (
    [26th of February, 2013] => Array (
     [0] All That Glitters - This Sound (Felix Cartal Remix) 
     [1] Bertie Blackman - Hide and Seek 
     [2] City Calm Down - Sense of Self 
     [3] Cosmo Jarvis - My Own Thing 
    ) 
    [18th of December, 2012] => Array (
     [0] Birds of Tokyo - This Fire 
     [1] Bloc Party - Truth 
     [2] C2C - Down the Road 
     [3] Chance Waters - Young and Dumb feat Bertie Blackman 
     [4] Clubfeet - Heartbreak 
    ) 
    [6th of June, 2013] => Array (
     [0] Courtney Barnett - History Eraser 
     [1] Delphic - Baiya 
    ) 
) 

有人可以請幫助我,我該如何做到這一點?

我試圖實現的主要目標是能夠將文件放在我的服務器上的文件夾中,並將我的腳本打印出來,形成一個<ul>,按上傳日期分組。

<ul> 
    Last Updated: 26th of February, 2013 
    <li>All That Glitters - This Sound (Felix Cartal Remix)</li> 
    <li>Bertie Blackman - Hide and Seek</li> 
    <li>City Calm Down - Sense of Self</li> 
    <li>Cosmo Jarvis - My Own Thing</li> 
</ul> 
<ul> 
    Last Updated: 18th of December, 2012 
    <li>Birds of Tokyo - This Fire</li> 
    <li>Bloc Party - Truth</li> 
    <li>C2C - Down the Road</li> 
    <li>Chance Waters - Young and Dumb feat Bertie Blackman</li> 
    <li>Clubfeet - Heartbreak</li> 
</ul> 
<ul> 
    Last Updated: 6th of June, 2013 
    <li>Courtney Barnett - History Eraser</li> 
    <li>Delphic - Baiya</li> 
</ul> 

謝謝大家誰試圖幫助我了這一點。

回答

7
foreach($files as $file){ 
    $path_parts = pathinfo($file); 
    $date = date ("jS \of F, Y", filemtime($file)); 
    if(isset($dateUploaded[$date])) { 
      $dateUploaded[$date][] = $path_parts['filename']; 
    } else { 
      $dateUploaded[$date] = array($path_parts['filename']); 
    } 
} 
0

分組

$groups = array(); 
foreach ($dateUploaded as $du) { 
    $key = $du['Date Uploaded']; 
    if (!isset($groups[$key])) $groups[$key] = array(); 
    $groups[$key][] = $du['File Name']; 
} 

輸出列表

$ret = ''; 
foreach ($groups as $key=>$list) 
    $ret.="<ul>\n".$key."<li>".implode("</li>\n<li>",$list)."</li></ul>"; 
0

快速代碼:

$arr = array(); 
foreach ($files as $file) { 
    $date = $file['Date Uploaded']; 
    $name = $file['File Name']; 

    $arr[$date][] = $name; 
} 
0

這也應該工作:

$newArray = array(); 
foreach($dateUploaded as $row) 
{ 
    $newArray[$row['Date Uploaded']][] = $row['File Name'] 
} 

print_r($newArray);