note 128177 added to function.array-reduce
| From: | xyz at record dot contact | Date: | Wed, 01 Feb 2023 18:16:37 +0000 |
| Subject: | note 128177 added to function.array-reduce | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220133@lists.php.net to get a copy of this message | ||
If you want to reindex an array, say to add an ID to each item as a key, you can use the following:
$array = [['id' => 3, 'name' => 'bob'], ['id' => 4,
'name' => 'alice']];
array_reduce($array, fn($new_array, $item) => $new_array + [$item['id'] => $item],
[]);
Result:
[ 3 => ["id" => 3, "name" => "bob"], 4 => ['id'
=> 4, 'name' => 'alice'] ]
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.158.213 (proxied: 185.59.221.226)
----
Manual Page -- https://php.net/manual/en/function.array-reduce.php
Edit -- https://main.php.net/note/edit/128177
Del: integrated -- https://main.php.net/note/delete/128177/integrated
Del: useless -- https://main.php.net/note/delete/128177/useless
Del: bad code -- https://main.php.net/note/delete/128177/bad+code
Del: spam -- https://main.php.net/note/delete/128177/spam
Del: non-english -- https://main.php.net/note/delete/128177/non-english
Del: in docs -- https://main.php.net/note/delete/128177/in+docs
Del: other reasons-- https://main.php.net/note/delete/128177
Reject -- https://main.php.net/note/reject/128177
Search -- https://main.php.net/manage/user-notes.php