3 ms·
Conspicuously absent from PHP: array_copy() ... in order to copy-by-value on arrays containing references. Currently to do that you have to do a very fragi
by snorkel 14y ago
Conspicuously absent from PHP:
array_copy()
... in order to copy-by-value on arrays containing references. Currently to do that you have to do a very fragile hack like this:
$copy = array_flip(array_flip($original));
... which is vulnerable to key-value collisions ... or you have to write your own homebrew array copy-by-value function.
- TazeTSchnitzel 14y agoFile a bug report :)
- smsm42 14y agoBetter yet, file an RFC and a pull request.
- TazeTSchnitzel 14y agoYep. That's what I'm trying to do for something else :D
- nieksand 14y agoStill ugly, but at least it won't crap out if your data has duplicate values: $copy = array_map( function($v) { return $v; }, $original );
- ircmaxell 14y agoVery simple. Either: $copy = $original; Or, if you must have a function, function array_copy(array $a) { return $a; } Arrays use the normal copy-on-write semantics of PHP. They are not passed by reference or object handle... The only potential issue is if the array contains references deeper in.