note 92193 added to function.array-slice
| From: | ASaed at osu1 dot php dot net | Date: | Mon, 13 Jul 2009 21:46:29 +0000 |
| Subject: | note 92193 added to function.array-slice | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158251@lists.php.net to get a copy of this message | ||
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)
----
Server IP: 64.71.164.2
Probable Submitter: 209.68.185.23
----
Manual Page -- http://www.php.net/manual/en/function.array-slice.php
Edit -- https://master.php.net/note/edit/92193
Del: integrated -- https://master.php.net/note/delete/92193/integrated
Del: useless -- https://master.php.net/note/delete/92193/useless
Del: bad code -- https://master.php.net/note/delete/92193/bad+code
Del: spam -- https://master.php.net/note/delete/92193/spam
Del: non-english -- https://master.php.net/note/delete/92193/non-english
Del: in docs -- https://master.php.net/note/delete/92193/in+docs
Del: other reasons-- https://master.php.net/note/delete/92193
Reject -- https://master.php.net/note/reject/92193
Search -- https://master.php.net/manage/user-notes.php