PHP: How to compare keys in one array with values in another, and return matches?

前端 未结 4 1963
终归单人心
终归单人心 2020-12-30 06:10

I have the following two arrays:

$array_one = array(\'colorZero\'=>\'black\', \'colorOne\'=>\'red\', \'colorTwo\'=>\'green\', \'colorThree\'=>\'b         


        
4条回答
  •  一整个雨季
    2020-12-30 06:38

    As of PHP 5.1 there is array_intersect_key (manual).

    Just flip the second array from key=>value to value=>key with array_flip() and then compare keys.

    So to compare OP's arrays, this would do:

    $result = array_intersect_key( $array_one , array_flip( $array_two ) );
    

    No need for any looping the arrays at all.

提交回复
热议问题