array_flip_PHP array_flip()函数与示例

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值