Extract structures from PHP arrays, kind of like an advanced pluck
.
The provided xtract
function takes a $source
(usually an array) and a $target
, which is the structure to transform the $source
into:
mixed xtract( mixed $source, mixed $target )
xtract([ 1, 2, 3 ], [ 0, 1 ]) === [ 1, 2 ];
// The asterisk is a special token, translating to "anything"
xtract([ 1, 2, 3 ], '*') === [ 1, 2, 3 ];
$source = [
'a' => [
'd' => 3,
'e' => 4
],
'b' => [
'd' => 5,
'e' => 6
],
'c' => [
'd' => 7,
'e' => 8
]
];
xtract($source, [ 'a' ])
===
[
'a' => [
'd' => 3,
'e' => 4
]
];
xtract($source, [ '*' => [ 'd' ] ])
===
[
'a' => [ 'd' => 3 ],
'b' => [ 'd' => 5 ],
'c' => [ 'd' => 7 ]
];