Home  >  Article  >  Backend Development  >  What function does PHP use to exchange array keys and values?

What function does PHP use to exchange array keys and values?

青灯夜游
青灯夜游Original
2022-08-23 19:41:152448browse

php uses the "array_flip()" function to exchange array keys and values. The array_flip() function is used to exchange the key names in the array and the corresponding associated key values. The syntax is "array_flip(array);". The parameter array represents the array to be exchanged for key and value pairs; if the exchange is successful, the exchanged array is returned. Returns NULL if the exchange fails.

What function does PHP use to exchange array keys and values?

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php is used to exchange array keys and values "array_flip()" function.

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

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

Return value: If the reversal is successful, the reversed array is returned; if the reversal fails, NULL is returned.​

Note: We must remember that the values ​​of the array must be valid keys, i.e. they must be integers or strings. If a value is of the wrong type, a warning will be thrown and the associated key/value pair will not be included in the result.

Example

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;); 
$arr=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
var_dump($arr);
$result=array_flip($arr);
var_dump($result);
?>

What function does PHP use to exchange array keys and values?

Description: Use the array_flip() function to remove duplicate elements from the array

In PHP, the key name of the array is unique and will not exist repeatedly.

Using this feature, if two values ​​in the array are the same, the last key and value will be retained after reversing the array, which can indirectly achieve deduplication of the array.

<?php
$arr = array("a"=>"a1","b"=>&#39;b1&#39;,"c"=>"a2","d"=>"a1");
$arr1 = array_flip($arr);
var_dump($arr1);//先反转一次,去掉重复值,输出Array ( [a1] => d [b1] => b [a2] => c )
$arr2 = array_flip($arr1);
var_dump($arr2);//再反转回来,得到去重后的数组,输出Array ( [a] => a1 [b] => b1 [c] => a2 )
$arr3 = array_unique($arr);
var_dump($arr3);//利用php的array_unique函数去重,输出Array ( [a] => a1 [b] => b1 [c] => a2 )
?>

What function does PHP use to exchange array keys and values?

The difference between the two methods is that array_flip gets the last key and value of the repeated element, and array_unique gets the first key and value of the two repeated elements. .

Extended knowledge: Without using a function, you can also use the foreach statement and an empty array to exchange array keys and values

<?php
$arr = array("a"=>"a1","b"=>&#39;b1&#39;,"c"=>"a2","d"=>"a1");
var_dump($arr);
$res=[];
foreach($arr as $k=>$v){
	$res[$v]=$k;
}
var_dump($res);
?>

What function does PHP use to exchange array keys and values?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What function does PHP use to exchange array keys and values?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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