Slice a array (like MySQLs limit)
This example shows how you can slice a array to a specific size like you would use the MySQL limit command.
Author:
Jonas John
License:
Public Domain
Language:
PHP
Created:
03/28/2006
Updated:
10/29/2007
Tags:
array functions
// Example usage of the build-in array slice method: $ExampleArray = array(1, 2, 3, 4, 5, 6); $ResultArray = array_slice($array, 0, 4); print_r($ResultArray); /* Returns: array(1, 2, 3, 4) */ /* ** My old method (deprecated) ** ** I created this method before I found the array_slice method. */ function ArrayLimit($InputArray, $From, $Length=0){ $NewArray = array(); $Length = ($Length==0) ? count($InputArray) - $From : $Length; $c = 0; reset($InputArray); while(list($k,$v) = each($InputArray)){ if ($c >= $From && $c < $Length) $NewArray[$k] = $v; else break; $c++; } return $NewArray; }
Feel free to leave a message:
Add a comment