Array questions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Array questions

Post by toasty2 »

I have two problems with my php script that lists files in a directory, it shows blank filesnames for some of the listing (why?). And, with the webbrowser I use to display the directory list, it cant show all of the listing (no scrolling is allowed in this program) so I need to split the array into 3 parts so I can have 3 columns, each column in a different array and I need to remove the empty filenames from the array. So far I have:

Code: Select all

<?
$path = "/homepages/21/d155481990/htdocs/server";
$dir_handle = @opendir($path) or die("Unable to open $path");
while ($file = readdir($dir_handle)) 
{
   if($file!="." && $file!=".." && $file!="")
$newfile = str_replace(".mp3", "", $file);
      echo "$newfile<br/>";
}
closedir($dir_handle);
?>
And yes, it removes .mp3 from the name before displaying it, This is for a special program.

How can I split $newfile into 3 arrays?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You should look at glob(). It's much easier & nicer to get all the files from a directory.

As for splitting up your array into 3 arrays, look at array_slice()
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

array_chunk() may be better suited for the splitting.. alternately, some clever math with a linear array works well too. Useful Posts has links -- the first two -- to possible mappings to help you output the data, depending on what direction and complexity you're going for.
Post Reply