Jump to content

Archived

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

Fearpig

Find top value in an array

Recommended Posts

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(); //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>";
}
[/code]

Share this post


Link to post
Share on other sites
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 here
echo $value;
$count++;
}
}[/code]?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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(); //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];
[/code]

Share this post


Link to post
Share on other sites
Hi,

Change: [code=php:0]echo $thefiles[$number_holder];[/code]

to: [code=php:0]echo $thefiles[$number_holder]['filename'];[/code]


Regards
Huggie

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
You are correct, it does.

There is also another variant to this solution..

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

?>[/code]

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.