DotDragnet
May 21, 2012, 04:37:42 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: follow us on twitter @dotdragnet
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: PHP array_chunk  (Read 1417 times)
Jeep Stone
Hero Member
*****
Posts: 908



View Profile WWW Awards
« on: October 23, 2007, 04:21:24 PM »

My understanding of the array_chunk method, returns a multidimensional array, chunked by an array size. In the following code, I want to return a certain set of URLs (a page worth), but it returns all of them. Do I need to remove the other values from my array?

Code:
function getImages($currentPage) {
$images = array();
try {
  if (is_dir(self::FOLDER)) {
if ($dh = opendir(self::FOLDER)) {
while (($filename = readdir($dh)) !== false) {
if (($filename != ".") && ($filename != "..")) {
$image = self::FOLDER . "/" . $filename;
array_push($images, $image);
}
}
closedir($dh);
}
}
}
catch (Exception $ex)
{


}
sort($images, SORT_STRING);
array_chunk($images, 5, true);
return $images;
}
Logged

JasonD
Global Moderator
Hero Member
*****
Posts: 546



View Profile Awards
« Reply #1 on: October 23, 2007, 05:10:03 PM »

Quote
array_chunk($images, 5, true);
return $images;

Either
Code:
return $images[$currentPage - 1];

But there is no point chunking if you only want one piece, replace with

Code:
return array_slice($images, 5 * ($currentPage - 1), 5);

If your page numbering starts from 0 take out the - 1.
Logged
Jeep Stone
Hero Member
*****
Posts: 908



View Profile WWW Awards
« Reply #2 on: October 24, 2007, 07:47:31 AM »

Super! Thanks for your help on this Jason.
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!