note 113369 added to function.array-filter
| From: | qeremy | Date: | Wed, 02 Oct 2013 15:12:04 +0000 |
| Subject: | note 113369 added to function.array-filter | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196447@lists.php.net to get a copy of this message | ||
Here is key-passed array_filter function.
<?php
function arrayfilter(array $array, callable $callback = null) {
if ($callback == null) {
$callback = function($key, $val) {
return (bool) $val;
};
}
$return = array();
foreach ($array as $key => $val) {
if ($callback($key, $val)) {
$return[$key] = $val;
}
}
return $return;
}
$test_array = array('foo', 'a' => 'the a', 'b' =>
'the b', 11 => 1101, '', null, false, 0);
$array = arrayfilter($test_array, function($key, $val) {
return is_string($key);
});
print_r($array);
/*
Array
(
[a] => the a
[b] => the b
)
*/
$array = arrayfilter($test_array);
print_r($array);
/*
Array
(
[0] => foo
[a] => the a
[b] => the b
[11] => 1101
)
*/
?>
----
Server IP: 69.147.83.199
Probable Submitter: 78.181.129.63
----
Manual Page -- http://php.net/manual/en/function.array-filter.php
Edit -- https://master.php.net/note/edit/113369
Del: integrated -- https://master.php.net/note/delete/113369/integrated
Del: useless -- https://master.php.net/note/delete/113369/useless
Del: bad code -- https://master.php.net/note/delete/113369/bad+code
Del: spam -- https://master.php.net/note/delete/113369/spam
Del: non-english -- https://master.php.net/note/delete/113369/non-english
Del: in docs -- https://master.php.net/note/delete/113369/in+docs
Del: other reasons-- https://master.php.net/note/delete/113369
Reject -- https://master.php.net/note/reject/113369
Search -- https://master.php.net/manage/user-notes.php