Swapping Array

Swapping Array Keys and Values

In this article we discuss about Swapping Array Keys and Values.

If you want interchange the keys and values of an associative array. It’s very easy, just use PHP’s array_flip() function which perform a very specialized task. It reverses the key-value relationship for all the elements of an associative array and returning a new array that is the mirror image of the original.

<?php
$opposite_key_value = array(“white” => “black”, “day” => “night”, “open” => “close”);
print_r(array_flip($opposite_key_value));
?>

Output:

Exchanged keys and values

(“black” => “white”, “night” => “day”, “close” => “open”)

 

Related Posts


Leave a Reply

Your email address will not be published. Required fields are marked *


*