note 127009 added to function.array-column
| From: | Sbastien at main dot php dot net | Date: | Mon, 25 Apr 2022 07:02:25 +0000 |
| Subject: | note 127009 added to function.array-column | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-218311@lists.php.net to get a copy of this message | ||
The counterpart of array_column(), namely create an array from columns, can be done with array_map()
:
<?php
// Columns
$lastnames = ['Skywalker', 'Organa', 'Kenobi'];
$firstnames = ['Luke', 'Leia', 'Obiwan'];
// Columns to array
$characters = array_map(
fn ($l, $f) => ['lastname' => $l, 'firstname' => $f],
$lastnames, $firstnames
);
print_r($characters);
/*
[
0 => ['lastname' => 'Skywalker', 'firstname' =>
'Luke']
1 => ['lastname' => 'Organa', 'firstname' =>
'Leia']
2 => ['lastname' => 'Kenobi', 'firstname' =>
'Obiwan']
]
*/
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.135 (proxied: 82.66.138.226)
----
Manual Page -- https://php.net/manual/en/function.array-column.php
Edit -- https://main.php.net/note/edit/127009
Del: integrated -- https://main.php.net/note/delete/127009/integrated
Del: useless -- https://main.php.net/note/delete/127009/useless
Del: bad code -- https://main.php.net/note/delete/127009/bad+code
Del: spam -- https://main.php.net/note/delete/127009/spam
Del: non-english -- https://main.php.net/note/delete/127009/non-english
Del: in docs -- https://main.php.net/note/delete/127009/in+docs
Del: other reasons-- https://main.php.net/note/delete/127009
Reject -- https://main.php.net/note/reject/127009
Search -- https://main.php.net/manage/user-notes.php