note 129920 added to function.list
| From: | xmgr2@protonmail.com | Date: | Wed, 18 Dec 2024 08:15:39 +0000 |
| Subject: | note 129920 added to function.list | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221727@lists.php.net to get a copy of this message | ||
... and it also supports pushing items onto arrays.
I couldn't find any official documentation on this behavior, but it appears that you can
"push" elements onto arrays directly using array destructuring assignments.
<?php
$users = [
[1, 'Alice', 'alice@example.com', 'secret123'],
[2, 'Bob', 'bob@example.com', 'pass456'],
[3, 'Charlie', 'charlie@example.com', 'hunter678'],
];
$ids = [];
$names = [];
$emails = [];
$passwords = [];
foreach ($users as $user) {
[$ids[], $names[], $emails[], $passwords[]] = $user;
}
?>
For well-documented alternatives, consider using array_column(), which does quite the same:
<?php
$ids = array_column($users, 0);
$names = array_column($users, 1);
$emails = array_column($users, 2);
$passwords = array_column($users, 3);
?>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 2003:c2:3f13:5400:408f:b9e5:482b:ff7e)
----
Manual Page -- https://php.net/manual/en/function.list.php
Edit -- https://main.php.net/note/edit/129920
Del: integrated -- https://main.php.net/note/delete/129920/integrated
Del: useless -- https://main.php.net/note/delete/129920/useless
Del: bad code -- https://main.php.net/note/delete/129920/bad+code
Del: spam -- https://main.php.net/note/delete/129920/spam
Del: non-english -- https://main.php.net/note/delete/129920/non-english
Del: in docs -- https://main.php.net/note/delete/129920/in+docs
Del: other reasons-- https://main.php.net/note/delete/129920
Reject -- https://main.php.net/note/reject/129920
Search -- https://main.php.net/manage/user-notes.php