php - remove only those keys that are not present in another array -
i have 2 arrays
$arr1 = array( 'a' => array(some values here), 'b' => array(some more values here), 'c' => array(some more , more values here) );
and
$arr2 = array('a','c');
you can see $arr2 have 2 values (a,c)
i want keep keys in $arr1 values present in $arr1 (maintaining key-value association), , want remove other values not in $arr2
so, output be
$arr1 = array( 'a' => array(some values here), 'c' => array(some more , more values here) );
how can achieve this?
to rephrase bit, want find keys present in both arrays, , keep values first array. happens array_intersect_key
does:
$arr1 = array_intersect_key($arr1, $arr2)
note: haven't used function often; might need change second array like:
$arr2 = array('a' => 1, 'c' => 1);
to ensure they're treated keys rather values.
incorporating deceze's tip, do
$arr1 = array_intersect_key($arr1, array_flip($arr2))
which wouldn't require changing second array.
Comments
Post a Comment