I wrote this function to get a subset of an array. Does php have a built in function for this. I can't find one in the docs. Seems like a waste if I'm reinventing the wheel.
function array_subset($array, $keys) {
$result = array();
foreach($keys as $key){
$result[$key] = $array[$key];
}
return $result;
}
Subset of an array can be extracted by using filter method. The filter method takes a function that returns boolean value as the input.
The array_keys() is a built-in function in PHP and is used to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.
An array is subset of another array.
As array values can be other arrays, trees and multidimensional arrays are also possible. And : The key can either be an integer or a string. The value can be of any type.
array_diff_key
and array_intersect_key
are probably what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With