Limit an array to X number of items

I got help many moons ago with a snippet of PHP that reads the contents of a directory where the file names are yyyymmdd.pdf and returns a list as an array which gets output as an HTML list. Works great, but I need to modify it for another purpose, and limit the array to the most recent five “dates”.

Have searched and came up with “limit”, but all the references are to SQL queries and that doesn’t apply here.

Here’s my include that gets my list; need to modify this to limit to the most recent five in the directory:

[code]

    <? $the_array = Array(); $handle = opendir('images/stories/bulletins/2011/.'); while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != "index.html") { $the_array[] = $file; } } closedir($handle); sort ($the_array); reset ($the_array); // leave the beginning of the script like you had before - get an array of filenames foreach($the_array as $val) { // this is the part you should change: $filesplit = explode('.', $val); $timestamp = strtotime($filesplit[0]); $list = date('F j, Y',$timestamp); echo "
  • $list
  • "; } ?>
[/code]

Thanks much!

You could use:

[php]
$thearray[5];
[/php]

Thanks for the speedy reply!

I changed it to

$the_array[5] = $file;

and it only lists one file name, and a seemingly arbitrary one at that – the eighth most recent in the directory. (There happens to be one file added per week so the directory holds about 41 files at this point and the one that gets listed is August 21.)

Then I tried

$the_array[5];

and got no list.

Not sure I’m putting that in the right place?

Hm… that’s strange. I’ll look forward to finding out the problem! :wink:

Numeric Arrays
A numeric array stores each array element with a numeric index.

There are two methods to create a numeric array.

  1. In the following example the index are automatically assigned (the index starts at 0):

[php]
$cars=array(“Saab”,“Volvo”,“BMW”,“Toyota”);
[/php]

  1. In the following example we assign the index manually:

[php]
$cars[0]=“Saab”;
$cars[1]=“Volvo”;
$cars[2]=“BMW”;
$cars[3]=“Toyota”;
[/php]

(excerpt of 'http://www.w3schools.com/php/php_arrays.asp)

You could try using the following method:
[php]$array = array_slice($array,0,5);[/php]

I had to relocate the line and I changed it to this so it would give me the most recent five in descending order, and it worked perfectly. Thanks to all who replied. Here’s the end result:

[code]

    <? $handle = opendir('images/stories/bulletins/2011/.'); while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != "index.html") { $the_array[] = $file; } } closedir($handle); arsort ($the_array); reset ($the_array); $the_array = array_slice($the_array,0,5); // leave the beginning of the script like you had before - get an array of filenames foreach($the_array as $val) { // this is the part you should change: $filesplit = explode('.', $val); $timestamp = strtotime($filesplit[0]); $list = date('F j, Y',$timestamp); echo "
  • $list
  • "; } ?>
[/code]

This resulted in a reverse chronological list of the most recent five files (named YYYYMMDD.pdf) in the directory, just in case anyone else finds a need for the code.

Sponsor our Newsletter | Privacy Policy | Terms of Service