Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare variables foreach

This script scans the directory 'uploads' and list their subfolders. All subfolders has this structure

YYYY-MM-DD_hh:mm:ss_text

for example

  • 2013-03-18_23:59:59_cam1
  • 2013-03-18_09:22:12_cam1
  • 2013-03-17_19:05:02_cam2
  • 2013-03-17_12:30:28_cam4

I want to make separate UL TAGS on new day (position 9 and 10). Something like

<ul><li>2013-03-18_23:59:59_cam1</li><li>2013-03-18_09:22:12_cam1</li></ul>
<ul><li>2013-03-17_19:05:02_cam2</li><li>2013-03-17_12:30:28_cam4</li></ul>

I have no idea how to compare position 9 and 10 in a foreach statement and ask for help! Thank you!

Here is my script

<?php
// Name of directory 
$directory = "uploads/"; 
$action=opendir($directory);
    while($read=readdir($action)){
    $dat_array[] = $read;
    }   
    //sort array reverse
    rsort($dat_array);  
    foreach($dat_array as $read) {
        if(!preg_match("!(\.|\..)$!", $read)){      
echo '<ul><li><a href="dir.php?id='.$read.'"><span>'.$read.'</span><span></span></a></li></ul>'; 
        } 
    }   
?>
like image 406
Roman Avatar asked Jul 30 '26 07:07

Roman


1 Answers

Try this code :

// Name of directory 
$dat_array              = array();
$directory              = "uploads/"; 
$action                 = opendir($directory);
while($read             = readdir($action)){
  $exp                  = explode("_",$read);
  $dat_array[$exp[0]][] = $read;
}   
rsort($dat_array); 

foreach($dat_array as $val){
   echo "<ul>";
   foreach($val as $v){
      echo "<li>".$v."</li>";
   }
   echo "</ul>";
}
like image 188
Prasanth Bendra Avatar answered Aug 01 '26 20:08

Prasanth Bendra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!