array_flip
PHP array_filter()函数 (PHP array_filter() Function)
array_flip() function is used to flip the array keys and values, it returns an array by flipping all values as keys and all keys as values.
array_flip()函数用于翻转数组键和值,它通过翻转所有值作为键和翻转所有键作为值来返回数组。
Syntax:
句法:
array_flip(array) : array
Here, array is an input array. The function will return a new array by flipping keys as values and values as keys.
在这里, 数组是一个输入数组。 该函数将通过翻转键作为值和值作为键来返回一个新数组。
As we know, an array contains unique keys. If we flip the array and the values are common then the last value will be considered as key and its key will be considered as value.
众所周知,数组包含唯一键。 如果我们翻转数组并且值是公共的,则最后一个值将被视为键,而其键将被视为值 。
For example – there are two values are common "a" => "Hello" and "b" => "Hello", in this case second element's value will be key and its key will be value: "Hello" => "b".
例如–有两个共同的值“ a” =>“ Hello”和“ b” =>“ Hello” ,在这种情况下,第二个元素的值将是键,其键将是值: “ Hello” =>“ b “ 。
Examples:
例子:
Input:
$arr = array("name" => "Prem", "age" => 28,"city" => "Gwalior");
Function calling:
array_flip($arr);
Output:
Array
(
[Prem] => name
[28] => age
[Gwalior] => city
)
PHP code 1: Example with unique values.
PHP代码1:具有唯一值的示例。
<?php
// input array
$arr = array("name" => "Prem", "age" => 28,"city" => "Gwalior");
// flipping array
$temp = array_flip($arr);
print_r ($temp);
?>
Output
输出量
Array
(
[Prem] => name
[28] => age
[Gwalior] => city
)
PHP code 2: Example without unique values.
PHP代码2:没有唯一值的示例。
<?php
// persons array
$arr = array("a" => "Hello", "b" => "Hello", "c" => "Hi");
// flipping array
$temp = array_flip($arr);
print_r ($temp);
?>
Output
输出量
Array
(
[Hello] => b
[Hi] => c
)
翻译自: https://www.includehelp.com/php/array_flip-function-with-example.aspx
array_flip