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
- array_change_key_case
- array_chunk
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_key_ exists
- array_keys