PHP 8.3.21 Released!

Voting

: min(six, four)?
(Example: nine)

The Note You're Voting On

pauljamescampbell at gmail dot com
17 years ago
Here's my own take on an array slice method that preserves keys from an associative array.

<?php
/**
* Array slice function that preserves associative keys
*
* @function associativeArraySlice
*
* @param Array $array Array to slice
* @param Integer $start
* @param Integer $end
*
* @return Array
*/
function associativeArraySlice($array, $start, $end) {
// Method param restrictions
if($start < 0) $start = 0;
if(
$end > count($array)) $end = count($array);

// Process vars
$new = Array();
$i = 0;

// Loop
foreach($array as $key => $value) {
if(
$i >= $start && $i < $end) {
$new[$key] = $value;
}
$i++;
}
return(
$new);
}
?>

<< Back to user notes page

To Top