note 103036 added to function.array-filter

From: Date: Tue, 22 Mar 2011 05:10:26 +0000
Subject: note 103036 added to function.array-filter
Groups: php.notes 
Request: Send a blank email to php-notes+get-178067@lists.php.net to get a copy of this message
In response to the note submitted by "rolf at example dot com" on "08-Nov-2010 07:06" PHP versions prior to 5.3 do not support the use of Anonymous Functions (also known as "closures"). The snippet submitted by "rolf at example dot com" (when run in a version of PHP earlier than 5.3) will emit a Parse Error. I propose an alternative to the code submitted by "rolf at example dot com" (for those using PHP versions prior to 5.3). Note: I have not tested this snippet for efficiency. <?php $array = array (1, 3, 3, 5, 6); $my_value = array_fill(0, count($array), 3); $mapped_array = array_map(create_function('$element, $my_value', 'return ($element != $my_value) ? $element : \'\';'), $array, $my_value); $filtered_array = array_filter($mapped_array); print_r($filtered_array); ?> Outputs: Array ( [0] => 1 [3] => 5 [4] => 6 ) ---- Server IP: 69.147.83.197 Probable Submitter: 173.238.11.185 ---- Manual Page -- http://www.php.net/manual/en/function.array-filter.php Edit -- https://master.php.net/note/edit/103036 Del: integrated -- https://master.php.net/note/delete/103036/integrated Del: useless -- https://master.php.net/note/delete/103036/useless Del: bad code -- https://master.php.net/note/delete/103036/bad+code Del: spam -- https://master.php.net/note/delete/103036/spam Del: non-english -- https://master.php.net/note/delete/103036/non-english Del: in docs -- https://master.php.net/note/delete/103036/in+docs Del: other reasons-- https://master.php.net/note/delete/103036 Reject -- https://master.php.net/note/reject/103036 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#178067) next »