note 99975 added to function.array-filter

From: Date: Fri, 17 Sep 2010 14:25:26 +0000
Subject: note 99975 added to function.array-filter
Groups: php.notes 
Request: Send a blank email to php-notes+get-172677@lists.php.net to get a copy of this message
>> mchargue at usc dot edu If you want to pass a parameter to the callback function, there is better way than defining global variables in PHP 5.3 (also see http://ua.php.net/manual/en/functions.anonymous.php): <?php /** * This function works exactly like array_search, * but returns an array of all found values. */ function array_search_all($needle, array $haystack, $strict = false) { return array_filter($haystack, function($value) use ($needle, $strict) { if ($strict) { return $value === $needle; } else { return $value == $needle; } }); } $haystack = array( 0 => 1, 1 => 2, 2 => 3, 3 => true, 4 => false ); // Example 1: search for "3" var_export(array_search_all(3, $haystack)); // prints: // array ( // 2 => 3, // 3 => true, ( because 3 == true ) // 5 => true, // ) // Example 2: search for "3" in strict mode var_export(array_search_all(3, $haystack, true)); // prints: // array ( // 2 => 3, // ) ---- Server IP: 88.81.245.102 (proxied: 91.196.196.205 1.0 localhost.ua.elro.com:3128 (squid/2.7.STABLE7)) Probable Submitter: 213.227.246.32 ---- Manual Page -- http://www.php.net/manual/en/function.array-filter.php Edit -- https://master.php.net/note/edit/99975 Del: integrated -- https://master.php.net/note/delete/99975/integrated Del: useless -- https://master.php.net/note/delete/99975/useless Del: bad code -- https://master.php.net/note/delete/99975/bad+code Del: spam -- https://master.php.net/note/delete/99975/spam Del: non-english -- https://master.php.net/note/delete/99975/non-english Del: in docs -- https://master.php.net/note/delete/99975/in+docs Del: other reasons-- https://master.php.net/note/delete/99975 Reject -- https://master.php.net/note/reject/99975 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172677) next »