note 117812 added to function.array-walk
| From: | nelf86 at gmail dot com | Date: | Thu, 13 Aug 2015 11:09:30 +0000 |
| Subject: | note 117812 added to function.array-walk | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203329@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 72.52.91.14
Probable Submitter: 62.254.108.50
----
Manual Page -- http://php.net/manual/en/function.array-walk.php
Edit -- https://master.php.net/note/edit/117812
Del: integrated -- https://master.php.net/note/delete/117812/integrated
Del: useless -- https://master.php.net/note/delete/117812/useless
Del: bad code -- https://master.php.net/note/delete/117812/bad+code
Del: spam -- https://master.php.net/note/delete/117812/spam
Del: non-english -- https://master.php.net/note/delete/117812/non-english
Del: in docs -- https://master.php.net/note/delete/117812/in+docs
Del: other reasons-- https://master.php.net/note/delete/117812
Reject -- https://master.php.net/note/reject/117812
Search -- https://master.php.net/manage/user-notes.php