[PHP-NOTES] note 130391 added to function.array-column

[email protected] ("[email protected]") Thu, 26 Jun 2025 17:10:49 +0000 (UTC)
Newsgroups php.notes
Message-ID <[email protected]>
array_column is the only function out of array_values, array_merge, array_slice and array_column that does de-reference values. Also, it works recursive!

<?php
$values = [];
$values['a'] = [];
$values['a']['b'] = 'foo';

$references = [];
$references['a'] = &$values['a'];

$copyColumn = array_combine(array_keys($references), array_column($references, null));

$values['a']['b'] = 'baz';

echo $copyColumn['a']['b'] . PHP_EOL; // foo
echo $references['a']['b'] . PHP_EOL; // baz
?>

If you want to preserve the keys you can use

<?php
$dereferenced = array_combine(array_keys($references), array_column($references, null))
?>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 84.131.204.191)
----
Manual Page -- https://php.net/manual/en/function.array-column.php
Edit        -- https://main.php.net/note/edit/130391
Del: integrated  -- https://main.php.net/note/delete/130391/integrated
Del: useless     -- https://main.php.net/note/delete/130391/useless
Del: bad code    -- https://main.php.net/note/delete/130391/bad+code
Del: spam        -- https://main.php.net/note/delete/130391/spam
Del: non-english -- https://main.php.net/note/delete/130391/non-english
Del: in docs     -- https://main.php.net/note/delete/130391/in+docs
Del: other reasons-- https://main.php.net/note/delete/130391
Reject      -- https://main.php.net/note/reject/130391
Search      -- https://main.php.net/manage/user-notes.php