Jump to content

array() problem


wisewood

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.
Link to comment
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
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.