SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array_unique problem

    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.

    PHP Code:
    //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);
    }


    Last edited by vzwhaley; Aug 1, 2007 at 05:45.

  2. #2
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    something like this?
    PHP Code:
     foreach($files as $file){
         
    $file2 $file;
        
        
    $temp explode("_"$file2);
        
        
    $i 1;
        while (
    $i count($temp)){
            
            
    $Section $temp[$i];
            
            
    $i += 1;
            
    $i++;
            
            
    $Sections = array($Section);
            
    $newArray array_unique($Sections);
            
    print_r($Sections);
            } 
    my mobile portal
    ghiris.ro

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I ran that last post and the output I get is :
    Array ( [0] => A ) Array ( [0] => A ) Array ( [0] => A ) Array ( [0] => B ) Array ( [0] => B ) Array ( [0] => B ) Array ( [0] => C ) Array ( [0] => C )

    Try this :
    PHP Code:
    function getUniqueCharacters$in ) {
        
    $out = array();
        foreach(
    $in as $file){
            
    $temp explode("_"$file);
            if ( !
    in_array$temp[1], $out ) ) {
                
    $out[] = $temp[1];
            }
        }
        return 
    $out;

    Yields :
    Array ( [0] => A [1] => B [2] => C
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •