You want to flip an array's keys and values.
Use array_flip().
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php $fruits = array('Apples', 'Grapes', 'Bananas', 'Oranges'); print_r($fruits); $fruits = array_flip($fruits); echo "<p><b>Use array_flip() to flip the array</b></p>"; print_r($fruits); echo "<p><b>This is an associated array.</b></p>"; $fruits = array('red'=>'Apples', 'green'=>'Grapes', 'yellow'=>'Bananas', 'orange'=>'Oranges'); print_r($fruits); $fruits = array_flip($fruits); echo "<p><b>Flip the associated array.</b></p>"; print_r($fruits); ?> </p> </body> </html> |