note 117812 deleted from function.array-walk by cmb
| From: | cmb@php.net | Date: | Thu, 13 Aug 2015 13:05:35 +0000 |
| Subject: | note 117812 deleted from function.array-walk by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-203332@lists.php.net to get a copy of this message | ||
Note Submitter: nelf86 at gmail dot com
----
Here is how to perform an operation on some of the elements of an array:
<?php
$an_array = array(
'item1' => 0,
'item2' => 0,
'item3' => 0,
'item4' => 0,
'item5' => 0,
);
$items_to_modify = array('item1', "item3");
array_map(function ($value) use (&$an_array ) {
$an_array [$value] = (boolean)$an_array [$value]; //example operation:
}, $items_to_modify);
?>
This will take the original array and perform an action only on items specified on the second array
items. Use of & symbol in the use statement makes the array_map access the variable as a
reference in an outer scope.
This makes code easily extendable.