PHP数组键值怎么互换?

PHPz
Release: 2020-09-05 10:14:02
Original
7955 people have browsed it

在PHP中可以使用“array_flip()”函数来将数组的键值互换,其语法是“array_flip(array);”,参数array表示需进行键和值对反转的数组。

PHP数组键值怎么互换?

PHP数组键值怎么互换?

在PHP中,可以使用array_flip()函数来将数组的键值互换。

array_flip()函数用于反转/交换数组中的键名和对应关联的键值。

语法:

array_flip(array);
Copy after login
  • array:必需参数。规定需进行键/值对反转的数组。

返回值:

如果反转成功,则返回反转后的数组。如果反转失败,则返回 NULL。

示例:

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

输出:

Array ( [red] => a [green] => b [blue] => c [yellow] => d )
Copy after login

更多相关知识,请访问 PHP中文网!!

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 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!