note 96269 modified in function.array-map by danbrown
| From: | danbrown@php.net | Date: | Wed, 17 Feb 2010 22:34:47 +0000 |
| Subject: | note 96269 modified in function.array-map by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166097@lists.php.net to get a copy of this message | ||
I realize this function is easy enough to make, but this is a faster version (twice the speed) of [a
function] which I find incredibly useful.
<?php
function array_pluck($key, $input) {
if (is_array($key) || !is_array($input)) return array();
$array = array();
foreach($input as $v) {
if(array_key_exists($key, $v)) $array[]=$v[$key];
}
return $array;
}
?>
Usage:
<?php $ids = array_pluck('id', $users); ?>
--was--
I realize this function is easy enough to make, but this is a faster version (twice the speed) of
moester's (02-April-2008) array_pluck function, which I find incredibly useful.
function array_pluck($key, $input) {
if (is_array($key) || !is_array($input)) return array();
$array = array();
foreach($input as $v) {
if(array_key_exists($key, $v)) $array[]=$v[$key];
}
return $array;
}
Usage:
$ids = array_pluck('id', $users);
http://php.net/manual/en/function.array-map.php