Switching Keys For Values In PHP Arrays

February 2006:

I recently discovered the need to search an array for a value using PHP. The problem with the array_search() function is that it takes the value and an array as arguments and returns the key of the value found. Instead I had the key and needed to return the value. The problem was easily solved with the array_flip() function. The array_flip() function switches the key with the value. This allowed me to use the array_search() functions to retrieve the values I wanted.

$sample = array('key' => 'value');
                $sample = array_flip($sample);
                $sample_value = array_search('key', $sample);
                /* Will Return = 'value' */