I have made the following using standard PHP coding, involving loops and things. Though, I am sure I can accomplish the same algorithm using a combination of all PHP array functions: array_intersect*, array_fill*, and co...
I am hoping someone already encountered this and found a way, a fast way.
Ok, here it is:
I have an indexed array, let's call it data, it contains loads of informations about an entity, order doesn't matter obviously as it is an indexed array after all:
$data = [
'title' => 'loops and things',
'id' => '28fr2',
'link' => 'https://blah',
'itemid' => '28fr3',
'quantity' => 3,
//...
];
Now, I have a set of other arrays which describe a subset of my data array, for instance:
$product = [
'id',
'title',
'link'
];
and
$inventory = [
'itemid',
'quantity',
];
what I am after is something like:
array_intersect_key_while_we_keep_the_order($data, $product);
would give
[
'id' => '28fr2',
'title' => 'loops and things',
'link' => 'https://blah'
]
and
array_intersect_key_while_we_keep_the_order($data, $inventory);
would give
[
'itemid' => '28fr2',
'quantity' => 3
]
The problem I have with array_intersect_key is that the "order" is from the source array, not the one defining the list of keys. This is obviously straight forward using foreaches, but I am sure it can be done faster. I just can't see it.