Jump to content

Archived

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

wisewood

array() problem

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Hello,

Try this,

[code]
<?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.
?>
[/code]

hth

Share this post


Link to post
Share on other sites

×

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.