Home > php教程 > php手册 > body text

php-Arrays函数-array_flip-交换数组中键和值

WBOY
Release: 2016-06-13 10:49:43
Original
1324 people have browsed it

array_flip() 函数交换数组中的键和值

【功能】
         该函数将返回一个反转后的数组,
         即原数组的值变成了新数组的键值,原数组的键值变成了新数组的值
         如果数组中有相同的值,则只有最后的一个具有同样值的才会被反转到新数组
【使用范围】
         php4、php5.
【使用】
         array array_flip( array trans  )
         trans/必需/进行反转的数组
【示例】
[php]
$array1 = array( "blue" => 6, "red" => 2, "green" => 3, "purple" => 4 ); 
$array2 = array( "blue" => 6, "red" => 4, "green" => 6, "purple" => 4 ); 
$array3 = array( "blue" => 6, "red" => 4, "green" => 6, "purple" => '' ); 
 
print_r( array_flip( $array1 ) ); 
print_r( array_flip( $array2 ) ); 
print_r( array_flip( $array3 ) ); 
 
/*
Array
(
    [6] => blue
    [2] => red
    [3] => green
    [4] => purple
)
Array
(
    [6] => green
    [4] => purple
)
Array
(
    [6] => green
    [4] => red
    [] => purple
)
*/ 

 


摘自 zuodefeng的笔记

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
Popular Recommendations
Popular Tutorials
More>
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!