I have an array that I need key/values from, and an array of indexes:
$reference = [
'a' => 'this is A',
'b' => 'here is B',
'c' => 'C here',
'd' => 'finally, d',
];
$indexArray = [1, 3];
I want my result to be:
$result = [
'b' => 'here is B',
'd' => 'finally, d',
];
Currently what I have is:
$result = array_intersect(
$reference,
array_map(
function ($index) { return array_values($reference)[$index]; },
$indexArray
)
);
This works, but I am wondering if there is a built-in function that can already do this.
I know array_slice can do something like this if the values are next to each other, but is there another function that can slice based on an array of indexes?
Edited
This actually doesn't work unless my $reference array in constant. So, again, good solution if $reference isn't constant?