Re: Undefined variables and the array append operator
| From: | Peter Bowyer | Date: | Wed, 30 Mar 2022 18:23:41 +0000 |
| Subject: | Re: Undefined variables and the array append operator | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-117462@lists.php.net to get a copy of this message | ||
On Wed, 30 Mar 2022 at 15:33, Guilliam Xavier <guilliam.xavier@gmail.com>
wrote:
> Not really a "compelling reason why we should keep this inconsistency", but
> I have occasionally relied on array autovivification *for sub-dimensions*,
>
I rely on this often when remapping data for analysis. These scripts are
run a handful of times and discarded. Autovivication keeps the code short.
Here's a snippet I wrote yesterday:
$out = [];
while ($row = $res->fetchAssociative()) {
// ...
$docId = $row['document_id'];
if (!isset($out[$docId])) {
$out[$docId] = [
'application_id' => $row['application_id'],
'document_id' => $docId,
'filename' => $row['filename'],
];
}
$out[$docId]['labels'][$row['document_rejection_reason_id']] =
true;
}
Naturally I would prefer to keep this behaviour for arrays.
Peter