array_flipEXCHANGES all keys for their respective values in an ARRAY.
This function returns an array in flip order, i.e. keys from $array become values and values from array become keys.
Note that the values of $array need to be valid keys, i.e. they need to be either INTEGER or STRING.
A Warning will be emitted if a value has the wrong type, and the key/value pair in question will not be included in the result.
If a value has several occurrences, the latest key will be used as its value, and all others will be lost.
This function returns NULL on FAILURE.
<?php
arr array_flip ( arr $array )
where,
$array = The input ARRAY which key/value will be flipped
?>
$array
The given array where key/value pairs to be flipped.
EXERCISE
<?php
$arr01d = ["R", "G", "B", "RED", "GREEN", "BLUE"];
print_r($arr01d);
echo '<br><br>';
$arr01f = array_flip($arr01d);
print_r($arr01f);
?>
RESULT
INITIAL ARRAY:
Array ( [0] => R [1] => G [2] => B [3] => RED [4] => GREEN [5] => BLUE )
FLIPPED ARRAY:
Array ( [R] => 0 [G] => 1 [B] => 2 [RED] => 3 [GREEN] => 4 [BLUE] => 5 )
EXERCISE
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
WHEN THERE ARE ELEMENTS LIKE ARRAYS,
FATALLY, SOMETHING CAN GO WRONG!
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
$var02 = array(1 => array(0,1), 2 => array(0,2), 3 => "three");
$arrflipped02 = array_flip($var02);
if($arrflipped02)
{
echo 'The given ARRAY:<br>';
print_r($var02);
echo '<br>The flipped ARRAY:<br>';
print_r($arrflipped02);
}
else
{
echo 'The flip is not possible!';
}
?>
EXERCISE
<?php
$trans = [ "a" => 1,
"b" => 1,
"c" => 2,
"z" => 0,
"d" => TRUE,
"E" => FALSE,
"F" => NULL,
0 => "G",
1 => "h",
2 => "i" ];
$trans = array_flip($trans);
var_dump($trans);
?>
EXERCISE
<?php
echo "Testing array_flip() : basic functionality<br><br>";
// array with default keys - numeric values
$input = array(1, 2);
echo 'The given ARRAY:<br>';
var_dump($input);
echo '<br>The flipped ARRAY:<br>';
var_dump( array_flip($input) );
echo '<br><br>';
// array with default keys - string values
$input = array('value1', "value2");
echo 'The given ARRAY:<br>';
var_dump($input);
echo '<br>The flipped ARRAY:<br>';
var_dump( array_flip($input) );
echo '<br><br>';
// associative arrays - key as string
$input = array('key1' => 1, "key2" => 2);
echo 'The given ARRAY:<br>';
var_dump($input);
echo '<br>The flipped ARRAY:<br>';
var_dump( array_flip($input) );
echo '<br><br>';
// associative arrays - key as numeric
$input = array(1 => 'one', 2 => "two");
echo 'The given ARRAY:<br>';
var_dump($input);
echo '<br>The flipped ARRAY:<br>';
var_dump( array_flip($input) );
echo '<br><br>';
// combination of associative and non-associative array
$input = array(1 => 'one','two', 3 => 'three', 4, "five" => 5);
echo 'The given ARRAY:<br>';
var_dump($input);
echo '<br>The flipped ARRAY:<br>';
var_dump( array_flip($input) );
echo '<br><br>';
?>
EXERCISE
<?php
/*
* Trying different keys in $input array argument
*/
echo "Different keys for 'input' array argument<br><br>";
// different heredoc strings
$empty_heredoc = <<<EOT1
EOT1;
$simple_heredoc = <<<EOT4
simple
EOT4;
$multiline_heredoc = <<<EOT7
multiline heredoc with 123 and
speci@! ch@r$...checking\nand\talso
EOT7;
$input = array(
// default key
'one',
//expected: default key 0,
// value will be replaced by 'bool_key4'
// numeric keys
1 => 'int_key',
// expected: value will be replaced by 'bool_key3'
-2 => 'negative_key',
8.9 => 'float_key',
012 => 'octal_key',
0x34 => 'hex_key',
// string keys
'key' => 'string_key1',
"two" => 'string_key2',
'' => 'string_key3',
"" => 'string_key4',
" " => 'string_key5',
// bool keys
true => 'bool_key1',
false => 'bool_key2',
TRUE => 'bool_key3',
FALSE => 'bool_key4',
// null keys
null => 'null_key1',
NULL => 'null_key2',
// expected: value will be replaced by 'null_key2'
// binary key
"a".chr(0)."b" => 'binary_key1',
b"binary" => 'binary_key2',
//heredoc keys
$empty_heredoc => 'empty_heredoc',
$simple_heredoc => 'simple_heredoc',
$multiline_heredoc => 'multiline_heredoc',
);
echo '<pre>';
var_dump( array_flip($input) );
echo '</pre>';
?>
EXERCISE
<?php
/*
* In 'input' array argument,
* values are expected to be valid keys
* i.e. string/integer
* here testing for all different
* valid string and integer values
*/
echo "Different valid values in 'input' array argument:<br>";
$empty_heredoc = <<<EOT1
EOT1;
$simple_heredoc = <<<EOT4
simple
EOT4;
$multiline_heredoc = <<<EOT7
multiline heredoc with 123 and
speci@! ch@r$..checking\nwith\talso
EOT7;
$input = array(
// numeric values
'int_value' => 1,
'negative_value' => -2,
'zero_value' => 0,
'octal_value' => 012,
'hex_value' => 0x23,
// single quoted string value
'empty_value1' => '',
'space_value1' => ' ',
'char_value1' => 'a',
'string_value1' => 'string1',
'numeric_value1' => '123',
'special_char_value1' => '!@#$%',
'whitespace1_value1' => '\t',
'whitespace2_value1' => '\n',
'null_char_value1' => '\0',
// double quoted string value
'empty_value2' => "",
'space_value2' => " ",
'char_value2' => "b",
'string_value2' => "string2",
'numeric_value2' => "456",
'special_char_value2' => "^&*",
'whitespace1_value2' => "\t",
'whitespace2_value2' => "\n",
'null_char_value2' => "\0",
'binary_value' => "a".chr(0)."b",
// heredoc string value
'empty_heredoc' => $empty_heredoc,
'simple_heredoc' => $simple_heredoc,
'multiline_heredoc' => $multiline_heredoc,
);
echo '<pre>';
var_dump( array_flip($input) );
echo '</pre>';
?>
EXERCISE
<?php
/*
* Using different types of repeatitive keys
* as well as values for 'input' array
*/
echo "'input' array with repeatitive keys/values<br><br>";
// array with numeric key repeatition
$input = [1 => 'value', 2 => 'VALUE',
1 => "VaLuE", 3.4 => 4, 3.4 => 5];
echo 'The given ARRAY:<br>';
var_dump($input);
echo '<br>The flipped ARRAY:<br>';
var_dump( array_flip($input) );
echo '<br><br>';
// array with string key repeatition
$input =["key" => 1, "two" => 'TWO', 'three' => 3, 'key' => "FOUR"];
echo 'The given ARRAY:<br>';
var_dump($input);
echo '<br>The flipped ARRAY:<br>';
var_dump( array_flip($input) );
echo '<br><br>';
// array with bool key repeatition
$input = array(true => 1, false => 0, TRUE => -1);
echo 'The given ARRAY:<br>';
var_dump($input);
echo '<br>The flipped ARRAY:<br>';
var_dump( array_flip($input) );
echo '<br><br>';
// array with null key repeatition
$input = array(null => "Hello", NULL => 0);
echo 'The given ARRAY:<br>';
var_dump($input);
echo '<br>The flipped ARRAY:<br>';
var_dump( array_flip($input) );
echo '<br><br>';
// array with numeric value repeatition
$input = ['one' => 1, 'two' => 2, 3 => 1, "index" => 1];
echo 'The given ARRAY:<br>';
var_dump($input);
echo '<br>The flipped ARRAY:<br>';
var_dump( array_flip($input) );
echo '<br><br>';
//array with string value repeatition
$input = ['key1' => "value1", "key2" => '2', 'key3' => 'value1'];
echo 'The given ARRAY:<br>';
var_dump($input);
echo '<br>The flipped ARRAY:<br>';
var_dump( array_flip($input) );
?>