Jump to content


Photo

Find top value in an array


  • Please log in to reply
10 replies to this topic

#1 Fearpig

Fearpig
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 13 September 2006 - 02:30 PM

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!


$thefiles = array(); //initialize
if ($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 out

krsort($thefiles);
//arrange descending by filename

array_shift($thefiles);

foreach ($thefiles as $singlefile) {
	$Doc = $singlefile['filename'];
    echo "<a class='Body2' href='$Doc'>$Doc</a><br>"; 
}

"Whats wrong with the cat?" - Mrs Schrödinger

#2 jpadie

jpadie
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 13 September 2006 - 02:42 PM

how do you define "top value"?

#3 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 13 September 2006 - 02:47 PM

if you can order the array the way you'd like, can you not just do a loop like this:
$count=1;
for each($array as $varName => $value) {
if($count==1){
// build link here
echo $value;
$count++;
}
}
?
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#4 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 13 September 2006 - 02:49 PM

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]

#5 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 14 September 2006 - 06:22 AM

likewise
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#6 Fearpig

Fearpig
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 14 September 2006 - 08:47 AM

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?



$thefiles = array(); //initialize
if ($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 out

krsort($thefiles);
//arrange descending by filename

$number_holder = count($thefiles) - 1;  

echo $thefiles[$number_holder];

"Whats wrong with the cat?" - Mrs Schrödinger

#7 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 14 September 2006 - 08:56 AM

Hi,

Change:
echo $thefiles[$number_holder];

to:
echo $thefiles[$number_holder]['filename'];


Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#8 Fearpig

Fearpig
  • Members
  • PipPipPip
  • Advanced Member
  • 195 posts

Posted 14 September 2006 - 08:58 AM

Just like that....

Cheers Huggiebear  ;D
"Whats wrong with the cat?" - Mrs Schrödinger

#9 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 14 September 2006 - 12:04 PM

<?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']);

?>


#10 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 14 September 2006 - 12:08 PM

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  :-\

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#11 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 14 September 2006 - 12:11 PM

You are correct, it does.

There is also another variant to this solution..

<?php

while (($file = readdir($handle)) !== false)
{
    if (!in_array($file, array('.', '..')) && is_file($path . $ds . $file))
    {
        $final_file = array('filename' => $file);
    }
}

echo htmlentities($final_file['filename']);

?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users