Req #17090 [Opn->Csd]: Getting slices of associative arrays

From: Date: Mon, 23 Oct 2017 01:08:44 +0000
Subject: Req #17090 [Opn->Csd]: Getting slices of associative arrays
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-211846@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=17090&edit=1 ID: 17090 Updated by: kalle@php.net Reported by: chs at baltic-online dot de Summary: Getting slices of associative arrays -Status: Open +Status: Closed Type: Feature/Change Request Package: Arrays related Operating System: Solaris 5.8 PHP Version: 4.2.0 -Assigned To: +Assigned To: kalle Block user comment: N Private report: N New Comment: This can easily be implemented in userland: http://fi2.php.net/manual/en/function.array-slice.php#64122 However for any future reader that would propose this to be a part of the core, the best way around it would be to submit a PR and mail internals@, as our array functions are already quite large Previous Comments: ------------------------------------------------------------------------ [2017-03-17 23:01:00] pollita@php.net 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. ------------------------------------------------------------------------ [2002-05-08 06:04:34] 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! ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=17090&edit=1

« previous php.bugs (#211846) next »