Fearpig Posted September 13, 2006 Share Posted September 13, 2006 Hello everyone,Could someone take a quick look at my code for me? I'm trying to scan a folder and list the highest file (by filename) as a link to that file. I've managed to list them in descending order but when I add the array_shift() function it removes the top value instead of all the others.eg. I can list files called "f4.pdf, b5.pdf, a3.pdf, c1.pdf" in the order "f4.pdf, c1.pdf, b5.pdf, a3.pdf" and make them links to the relevant files, but I need it limit the results to the top value "f4.pdf". With the code below I get "c1.pdf, b5.pdf, a3.pdf".Hope thats not too confusing! [code]$thefiles = array(); //initializeif ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != "index.php" && $file != "test.php" && !is_dir($file)) { //use filesystem functions to get the filesize and other attributes RTFM $thefiles[] = array('filename' => $file); //used this way, it just adds the new item to the end of the array // it is creating an array of arrays } } closedir($handle);}//at this point the $thefiles array contains all the file info//access it using 2 indexes like this echo $thefiles[0]['filetype'];//use array functions to sort $thefiles array to your pleasure RTFM//use the foreach() to cycle through the $thefiles array and echo the files and other attributes outkrsort($thefiles);//arrange descending by filenamearray_shift($thefiles);foreach ($thefiles as $singlefile) { $Doc = $singlefile['filename']; echo "<a class='Body2' href='$Doc'>$Doc</a><br>"; }[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20619-find-top-value-in-an-array/ Share on other sites More sharing options...
jpadie Posted September 13, 2006 Share Posted September 13, 2006 how do you define "top value"? Quote Link to comment https://forums.phpfreaks.com/topic/20619-find-top-value-in-an-array/#findComment-91067 Share on other sites More sharing options...
gerkintrigg Posted September 13, 2006 Share Posted September 13, 2006 if you can order the array the way you'd like, can you not just do a loop like this:[code]$count=1;for each($array as $varName => $value) {if($count==1){// build link hereecho $value;$count++;}}[/code]? Quote Link to comment https://forums.phpfreaks.com/topic/20619-find-top-value-in-an-array/#findComment-91076 Share on other sites More sharing options...
Wintergreen Posted September 13, 2006 Share Posted September 13, 2006 If you've got an array with all the file names stored in it, do a $number_holder = count($array) - 1; If your highest file (f4.pdf) was the last element in the array, access it with $array[$number_holder] Quote Link to comment https://forums.phpfreaks.com/topic/20619-find-top-value-in-an-array/#findComment-91079 Share on other sites More sharing options...
gerkintrigg Posted September 14, 2006 Share Posted September 14, 2006 likewise Quote Link to comment https://forums.phpfreaks.com/topic/20619-find-top-value-in-an-array/#findComment-91535 Share on other sites More sharing options...
Fearpig Posted September 14, 2006 Author Share Posted September 14, 2006 OK that's really confused me!! I've added the code you mentioned but now when I refresh the page it just says "Array" (which is NOT one of the file names). Could one of you take a look and see if I've implemented the code incorrectly?[code]$thefiles = array(); //initializeif ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != "index.php" && $file != "test.php" && !is_dir($file)) { //use filesystem functions to get the filesize and other attributes RTFM $thefiles[] = array('filename' => $file); //used this way, it just adds the new item to the end of the array // it is creating an array of arrays } } closedir($handle);}//at this point the $thefiles array contains all the file info//access it using 2 indexes like this echo $thefiles[0]['filetype'];//use array functions to sort $thefiles array to your pleasure RTFM//use the foreach() to cycle through the $thefiles array and echo the files and other attributes outkrsort($thefiles);//arrange descending by filename$number_holder = count($thefiles) - 1; echo $thefiles[$number_holder];[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20619-find-top-value-in-an-array/#findComment-91597 Share on other sites More sharing options...
HuggieBear Posted September 14, 2006 Share Posted September 14, 2006 Hi,Change: [code=php:0]echo $thefiles[$number_holder];[/code]to: [code=php:0]echo $thefiles[$number_holder]['filename'];[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/20619-find-top-value-in-an-array/#findComment-91598 Share on other sites More sharing options...
Fearpig Posted September 14, 2006 Author Share Posted September 14, 2006 Just like that....Cheers Huggiebear ;D Quote Link to comment https://forums.phpfreaks.com/topic/20619-find-top-value-in-an-array/#findComment-91599 Share on other sites More sharing options...
Jenk Posted September 14, 2006 Share Posted September 14, 2006 [code]<?php$path = realpath('/path/to/directory');$ds = DIRECTORY_SEPARATOR;$handle = opendir($path);$file = array();while (($file = readdir($handle)) !== false){ if (!in_array($file, array('.', '..')) && is_file($path . $ds . $file)) { $files[] = array('filename' => $file); }}// get last array element.$lastFile = array_pop($files);echo htmlentities($lastFile['filename']);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20619-find-top-value-in-an-array/#findComment-91665 Share on other sites More sharing options...
HuggieBear Posted September 14, 2006 Share Posted September 14, 2006 Jenk's code will also work, but does something else behind the scenes that you aren't aware of.array_pop() actually removes the last item from the array. So although you get the value that you're after returned. Your $thefiles array now only contains 3 items instead of 4.... I think :-\RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/20619-find-top-value-in-an-array/#findComment-91669 Share on other sites More sharing options...
Jenk Posted September 14, 2006 Share Posted September 14, 2006 You are correct, it does.There is also another variant to this solution..[code]<?phpwhile (($file = readdir($handle)) !== false){ if (!in_array($file, array('.', '..')) && is_file($path . $ds . $file)) { $final_file = array('filename' => $file); }}echo htmlentities($final_file['filename']);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/20619-find-top-value-in-an-array/#findComment-91670 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.