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

From: Date: Fri, 17 Mar 2017 23:01:03 +0000
Subject: Req #17090 [Opn]: Getting slices of associative arrays
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207921@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: pollita@php.net Reported by: chs at baltic-online dot de Summary: Getting slices of associative arrays Status: Open Type: Feature/Change Request Package: Arrays related Operating System: Solaris 5.8 PHP Version: 4.2.0 Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [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 (#207921) next »