php - array_filter based on keys from another array -
i have 2 arrays:
$arr1 = array('a' => 10, 'b' => 20);
$arr2 = array('a' => 10, 'b' => 20, 'c' => 30);
how can use array_filter drop elements $arr2 don't exist in $arr1 ? "c" in example...
there function made purpose: array_intersect():
array_intersect — computes intersection of arrays
$arr2 = array_intersect($arr1, $arr2); if want compare keys, not values array_intersect(), use array_intersect_key():
array_intersect_key — computes intersection of arrays using keys comparison
$arr2 = array_intersect_key($arr1, $arr2); if want compare key=>value pairs, use array_intersect_assoc():
array_intersect_assoc — computes intersection of arrays additional index check
$arr2 = array_intersect_assoc($arr1, $arr2);
Comments
Post a Comment