Php-Arrays function-array_flip-exchange array keys and values

Source: Internet
Author: User

The array_flip () function exchanges keys and values in an array.

[Function]
This function returns an array after inversion,
That is, the value of the original array is changed to the key value of the new array, and the key value of the original array is changed to the value of the new array.
If the array has the same value, only the last one with the same value will be reversed to the new array.
Scope of use]
Php4, php5.
[Use]
Array array_flip (array trans)
Trans/required/inverted Array
[Example]
[Php]
<? 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
)
*/

 


Taken from zuodefeng's note

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.