Jump to content


Photo

array() problem


  • Please log in to reply
1 reply to this topic

#1 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 26 April 2006 - 11:27 AM

The title doesn't really do justice the the question to be honest.

I have an array, which is loaded up with all the filenames of files in a given directory.

Now, what i want to do, is create a drop down list using <SELECT>, which will let me specify which types of file i would like to view... ie .bmp, .php, .jpg, .asp, .txt etc etc.

I have ALOT of files in this directory, and want to be able to narrow it down somewhat by choosing what file types i want to browse.

I know i can use a substr() function to get the extension of the files, but can i then create an array only containing one entry for each of the file types, so i would end up with something like;

<SELECT>
<OPTION>asp</OPTION>
<OPTION>bmp</OPTION>
<OPTION>gif</OPTION>
<OPTION>jpg</OPTION>
<OPTION>php</OPTION>
<OPTION>txt</OPTION>
</SELECT>

I've already checked out the php.net/array pages and can't find anything that seems to be pointing me in the right direction.
wisewood: proven fact, I am both wise, and wooden.

#2 samshel

samshel
  • Members
  • PipPipPip
  • Advanced Member
  • 837 posts

Posted 26 April 2006 - 11:38 AM

Hello,

Try this,

<?php 

$filenames = array ("abc1.txt","abc2.jpg","abc3.png","abc4.txt");//this is the array which contains ur filenames
$extension_arr = array();
foreach($filenames as $key=>$val) {
    $arr = explode(".",$val);
    $extension = $arr[1];
    $extension_arr[$extension][] = $val;
}
$extensions = array_keys($extension_arr));//this array will contain all the extensions without duplicates
//array $extension_arr will contain extension wise filenames which can be used to display files belonging to a particular extension.
?>

hth
Cheers,
SamShel
-----------------------------------------------
--: FUTURE IS LINUX :--
-----------------------------------------------




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users