note 103036 deleted from function.array-filter by danbrown

From: Date: Tue, 22 Mar 2011 13:54:27 +0000
Subject: note 103036 deleted from function.array-filter by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-178080@lists.php.net to get a copy of this message
Note Submitter: Syd Miller ---- 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 )

« previous php.notes (#178080) next »