note 80909 modified in function.array-splice by thiago

From: Date: Thu, 24 Jul 2008 17:24:36 +0000
Subject: note 80909 modified in function.array-splice by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142673@lists.php.net to get a copy of this message
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); } ?> --was-- Here's my own take on an array slice method that preserves keys from an associative array. /** * 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); } http://php.net/manual/en/function.array-splice.php

« previous php.notes (#142673) next »