note 92193 deleted from function.array-slice by danbrown
| From: | danbrown@php.net | Date: | Mon, 13 Jul 2009 23:32:02 +0000 |
| Subject: | note 92193 deleted from function.array-slice by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-158254@lists.php.net to get a copy of this message | ||
Note Submitter: A Saed
----
Further to taylorbarstow below (for PHP 5), here's a key based slice for PHP 4 :
For numerically indexed arrays, you can re-index, as in
array_values(array_slice_keys($array,$keys))
<?php
// Slice an array based on a set of selection keys
function array_slice_keys($array,$keys) {
return array_flip(array_intersect(array_flip($array),$keys));
}
?>
Example:
$array =
array('a'=>'A','b'=>'B','c'=>'C',1=>'D',50=>'E');
$keys = array('a','b',50);
yields
array('a'=>'A','b'=>'B',50=>'E')
Example:
$array = array('A','B','C','D','E');
$keys = array(0,2,4);
Then array_values(array_slice_keys($array,$keys)) yields
array('A','C','E')
with indices 0,1,2 (and not 0,2,4)