php - How can I select a subset of values from an array using the values from another array as keys? -
this question has answer here:
- get array values keys 4 answers
 
here's array of $keys:
array (     [0] => 1     [1] => 3     [2] => 4 )   and $values:
array (     [0] => red     [1] => orange     [2] => yellow     [3] => green     [4] => blue )   i want create new array of of values in $values using values in $keys keys:
array (     [1] => orange     [3] => green     [4] => blue )   obviously can foreach values want, want make sure i'm not overlooking in plethora of php array functions.
i've googled question, , answer comes using array_combine, won't achieve desired output.
your appreciated :)
flip $keys array make values keys , use array_intersect_key():
$result = array_intersect_key($values, array_flip($keys));   returns values $values have same keys flipped $keys.
Comments
Post a Comment