note 89432 modified in function.array-filter by danbrown
| From: | danbrown@php.net | Date: | Sat, 07 Mar 2009 18:55:41 +0000 |
| Subject: | note 89432 modified in function.array-filter by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-151599@lists.php.net to get a copy of this message | ||
I wanted a function to keep the values that were filtered out and return them separately. Basically,
split the array by some callback. I wrote this for the job:
<?php
function array_split($input, &$trues, &$falses, $compare)
{
while (($item = array_pop($input)) != NULL)
{
if ($compare($item)) $trues[] = $item;
else $falses[] = $item;
}
}
?>
very simple, something I would think would be standard, and array_filter would amount to discarding
the falses.
--was--
I wanted a function to keep the values that were filtered out and return them separately. Basically,
split the array by some callback. I wrote this for the job:
function array_split($input, &$trues, &$falses, $compare)
{
while (($item = array_pop($input)) != NULL)
{
if ($compare($item)) $trues[] = $item;
else $falses[] = $item;
}
}
very simple, something I would think would be standard, and array_filter would amount to discarding
the falses.
http://php.net/manual/en/function.array-filter.php