note 76283 added to function.array-filter

From: Date: Mon, 09 Jul 2007 13:07:13 +0000
Subject: note 76283 added to function.array-filter
Groups: php.notes 
Request: Send a blank email to php-notes+get-128322@lists.php.net to get a copy of this message
Sometimes you want to do something with elements you have filtered out and something else with other elements. So instead of filtering elements out to one array you would like to split array into two parts: <?php /** * Splits array into two arrays using a callback function * * It is like array_filter(), but instead of one array, two arrays are returned: * first one with elements for which the callback function evaluates to true, * and second one, for which it evaluates to false. * * If no callback is supplied, all entries of input equal to FALSE will be removed. * * Array keys are preserved. * * @param $callback the callback function to use * @return array(truth_array, false_array) */ function array_split($input, $callback=null) { $callback = isset($callback) ? $callback : create_function('$x', 'return $x == true;'); $true = array(); $false = array(); foreach ($input as $key => $value) { if (call_user_func($callback, $value)) { $true[$key] = $value; } else { $false[$key] = $value; } } return array($true, $false); } ?> ---- Server IP: 212.47.220.13 Probable Submitter: 213.35.176.230 ---- Manual Page -- http://www.php.net/manual/en/function.array-filter.php Edit -- https://master.php.net/note/edit/76283 Del: integrated -- https://master.php.net/note/delete/76283/integrated Del: useless -- https://master.php.net/note/delete/76283/useless Del: bad code -- https://master.php.net/note/delete/76283/bad+code Del: spam -- https://master.php.net/note/delete/76283/spam Del: non-english -- https://master.php.net/note/delete/76283/non-english Del: in docs -- https://master.php.net/note/delete/76283/in+docs Del: other reasons-- https://master.php.net/note/delete/76283 Reject -- https://master.php.net/note/reject/76283 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128322) next »