Home  >  Article  >  php教程  >  php数组函数序列之array_flip() 将数组键名与值对调

php数组函数序列之array_flip() 将数组键名与值对调

WBOY
WBOYOriginal
2016-06-13 12:04:061563browse

array_flip() 定义和用法

array_flip() 函数将使数组的键名与其相应值调换,即键名变成了值,而值变成了键名。

array_flip() 函数返回一个反转后的数组,如果同一值出现了多次,则最后一个键名将作为它的值,所有其他的键名都将丢失。

如果原数组中的值的数据类型不是字符串或整数,函数将报错。

语法
array_flip(array)参数 描述
array 必需。规定输入的数组。

例子

复制代码 代码如下:


$a=array(0=>"Dog",1=>"Cat",2=>"Horse");print_r(array_flip($a));
?>


输出:

Array ( [Dog] => 0 [Cat] => 1 [Horse] => 2 )
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