|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-05-08 06:04 UTC] chs at baltic-online dot de
Hello,
unless I have overlooked something, there seems to be no easy way to get a slice of an associative array based on certain keys. I would like to be able to do something like the following:
$array["a"] = 1;
$array["b"] = 2;
$array["c"] = 3;
$slice = hash_slice($array, array("a", "c"));
which would result in $slice becoming an array containing the values 1 and 3, associated with the keys a and c, respectively.
If something like this already exists, I'd be happy if you could let me know; otherwise, I'd like to suggest this as a feature for a future version of PHP. Thank you!
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 04:00:01 2025 UTC |
A bit heavy perhaps, but: $array = array_filter($array, function ($k) { return in_array($k, ['a', 'c']); }, ARRAY_FILTER_USE_KEY); I'd probably be a bit hesitant to add such a task-specific utility function to the core, tbqh.