Jump to: navigation, search

Php array flip

From w3cyberlearnings

Contents

PHP function array_flip

This function exchanges array key with value, and value with key.

Syntax array_flip

  • array: array to exchange key to value and value to key.
array_flip(array);

Note

  • Duplicate key will not allow.

Example 1

<?php

$fruits = array('banana', 'apple', 'orange', 'coconut');
$flip_fruits = array_flip($fruits);
print_r($flip_fruits);
?>



Output

Array
(
    [banana] => 0
    [apple] => 1
    [orange] => 2
    [coconut] => 3
)

Example 2

<?php

$student_room = array('bob' => 'R302', 'janny' => 'G304', 'mark' => 'R303');
$room_student = array_flip($student_room);

foreach($room_student as $room=>$student) {
	echo "{$room} stays by {$student}<br/>";
}
?>


Output

R302 stays by bob
G304 stays by janny
R303 stays by mark

Related Links


Navigation
Web
SQL
MISC
References