PHP function array_flip() used to reverse/exchange the key names in the array and the corresponding associated key values

黄舟
Release: 2023-03-17 07:42:01
Original
2661 people have browsed it

Example

Reverse the key names in the array and the corresponding associated key values:

"red","b"=>"green","c"=>"blue","d"=>"yellow"); $result=array_flip($a1); print_r($result); ?>
Copy after login

Definition and usage

array_flip() function is used to reverse/exchange arrays The key name in and the corresponding associated key value.

Syntax

array_flip(array);
Copy after login
Parameters Description
array Required. Specifies the array whose key/value pairs need to be reversed.

Technical details

Return value: If the inversion is successful, the inversion is returned the array after. If the reversal fails, NULL is returned.
PHP version: 4+
## Note that the value in trans needs to be a legal key name , for example, it needs to be

integerorstring. A warning will be emitted if the value is of the wrong type, and the key/value pair in question will not be reversed.If the same value appears multiple times, the last key name will be used as its value, and all
otherswill be lost. array_flip() returns FALSE if it fails.
Example:

$hills=array("first"=>"data1″,"second"=>"data2″,"third"=>"data1″);   $hills=array_flip($hills); //还原键名   $hills1=array_flip(array_flip($hills));//删除重复   display $hills1
Copy after login
The result is:


data2 data1 two data.

The above is the detailed content of PHP function array_flip() used to reverse/exchange the key names in the array and the corresponding associated key values. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!