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
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 ) 
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.