Jump to content

[SOLVED] array_unique problem


vzwhaley

Recommended Posts

I have a directory of files with filenames such as 20070704_A_01.pdf, 20070704_B_03.pdf, etc.

 

I am trying to get the letter, such as A and B, exploded out of the filename. There will be several instances of the same letter, such as 5 letter A's and 6 letter B's, etc.

 

I want to get a unique list of all of the letters. But when I use array_unique, it does not recognize that there are multiple instances of the same letter. The array still looks like:

 

AAAAAAABBBBBBBBCCCCCCCCDDDDDDDD

 

Instead of:

 

ABCD

 

Any suggestions would be appreciated.

 

//The date is passed through a URL in the form of 2007_07_04

$Date = $_REQUEST['Date'];

$tempDate0 = explode("_", $Date);

$Year = $tempDate0[0];
$Month = $tempDate0[1];
$Day = $tempDate0[2];
  
$tempDate = $Month . "/" . $Day . "/" . $Year;

$dirName = $Year."/".$Month."/".$Day."/pdf/";

$dir = opendir($dirName);

$files = array();

if ($handle = $dir) {
   while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != ".." && !(is_dir($file))) {
           $files[] = $file;
       }
   }
} 

closedir($dir);
  
sort($files);


  foreach($files as $file) {
  	$file2 = $file;

$temp = explode("_", $file2);

$i = 0;
while ($i < count($temp)) {

$Section = $temp[$i + 1];

$i += 1;
$i++;

$Sections = array($Section);
$newArray = array_unique($Sections);
print_r($newArray);
}

}

Link to comment
https://forums.phpfreaks.com/topic/62835-solved-array_unique-problem/
Share on other sites

Im a little confused by the last part of your code. However, something like this should do what you are after:

 

<?php
$files = array('20070704_A_01.pdf','20070704_B_01.pdf','20070704_A_01.pdf');
$letters = array();
foreach($files as $v){
    $bits = explode('_',$v);
    $letters[] = $bits[1];
}
$letters = array_unique($letters);
print_r($letters);//with my array, output is Array ( [0] => A [1] => B ) 
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.