In PHP, you can use the array_flip function to reverse the key names and corresponding associated key values in the array. The syntax is "array_flip(array);". The parameter array specifies the array that needs to be reversed.
Recommendation: "PHP Video Tutorial"
array_flip() function is used to reverse/exchange elements in an array Key name and corresponding associated key value.
Syntax
array_flip(array);
Parameters
array required. Specifies the array whose key/value pairs need to be reversed.
Technical details
Return value: If the inversion is successful, the reversed array is returned. If the reversal fails, NULL is returned.
PHP version: 4
Example:
<?php $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $result=array_flip($a1); print_r($result); ?>
Run result:
Array ( [red] => a [green] => b [blue] => c [yellow] => d )
The above is the detailed content of How to reverse array values in php. For more information, please follow other related articles on the PHP Chinese website!