Does php have flip function?

[PHP 4, PHP 5, PHP 7, PHP 8]

array_flipExchanges all keys with their associated values in an array

Description

array_flip[array $array]: array

Note that the values of array need to be valid keys, i.e. they need to be either int 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.

Parameters

array

An array of key/value pairs to be flipped.

Return Values

Returns the flipped array.

Examples

Example #1 array_flip[] example

The above example will output:

Array
[
    [oranges] => 0
    [apples] => 1
    [pears] => 2
]

Example #2 array_flip[] example : collision

The above example will output:

Array
[
    [1] => b
    [2] => c
]

See Also

  • array_values[] - Return all the values of an array
  • array_keys[] - Return all the keys or a subset of the keys of an array
  • array_reverse[] - Return an array with elements in reverse order

Final

10 years ago

I find this function vey useful when you have a big array and you want to know if a given value is in the array. in_array in fact becomes quite slow in such a case, but you can flip the big array and then use isset to obtain the same result in a much faster way.

Tony H

9 years ago

This function is useful when parsing a CSV file with a heading column, but the columns might vary in order or presence:



I find this better than referencing the numerical array index.

Bob Ray

5 years ago

array_flip will remove duplicate values in the original array when you flip either an associative or numeric array. As you might expect it's the earlier of two duplicates that is lost:



Result:

array[3] {
  [0]  => string[3] "one"
  [1]  =>  string[3] "two"
  [2]  =>  string[3] "one"
}

array[2] {
  'one' => int[2]
  'two' => int[1]
}

This may be good or bad, depending on what you want, but no error is thrown.

snaury at narod dot ru

17 years ago

When you do array_flip, it takes the last key accurence for each value, but be aware that keys order in flipped array will be in the order, values were first seen in original array. For example, array:

    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 3
    [5] => 2
    [6] => 1
    [7] => 1
    [8] => 3
    [9] => 3

After flipping will become:
[first seen value -> first key]

    [1] => 7
    [2] => 5
    [3] => 9

And not anything like this:
[last seen value -> last key]

    [2] => 5
    [1] => 7
    [3] => 9

In my application I needed to find five most recently commented entries. I had a sorted comment-id => entry-id array, and what popped in my mind is just do array_flip[$array], and I thought I now would have last five entries in the array as most recently commented entry => comment pairs. In fact it wasn't [see above, as it is the order of values used]. To achieve what I need I came up with the following [in case someone will need to do something like that]:

First, we need a way to flip an array, taking the first encountered key for each of values in array. You can do it with:

  $array = array_flip[array_unique[$array]];

Well, and to achieve that "last comments" effect, just do:

  $array = array_reverse[$array, true];
  $array = array_flip[array_unique[$array]];
  $array = array_reverse[$array, true];

In the example from the very beginning array will become:

    [2] => 5
    [1] => 7
    [3] => 9

Just what I [and maybe you?] need. =^_^=

Prabhas Gupte

7 years ago

array_flip[] does not retain the data type of values, when converting them into keys. :[



This code outputs this:
array[3] {
  ["one"]=>
  string[1] "1"
  ["two"]=>
  string[1] "2"
  ["three"]=>
  string[1] "3"
}
array[3] {
  [1]=>
  string[3] "one"
  [2]=>
  string[3] "two"
  [3]=>
  string[5] "three"
}

It is valid expectation that string values "1", "2" and "3" would become string keys "1", "2" and "3".

pinkgothic at gmail dot com

15 years ago

In case anyone is wondering how array_flip[] treats empty arrays:



results in:

Array
[
]

I wanted to know if it would return false and/or even chuck out an error if there were no key-value pairs to flip, despite being non-intuitive if that were the case. But [of course] everything works as expected. Just a head's up for the paranoid.

kjensen at iaff106 dot com

10 years ago

I needed a way to flip a multidimensional array and came up with this function to accomplish the task.  I hope it helps someone else.

mmulej at gmail dot com

11 months ago

If you don't want to lose duplicates, and you're ok, with having the values in the flipped array in an array as well, you may use this:

PHP 7.4 - ^8



PHP 7.0 - ^7.3 [Time to upgrade to PHP 8 ^^]



PHP 5.4 - ^5.6 [Just don't]

dash

3 years ago

Notice : array_flip can turn string into integer

znailz at yahoo dot com

19 years ago

I know a lot of people want a function to remove a key by value from an array. I saw solutions that iterate[!] though the whole array comparing value by value and then unsetting that value's key. PHP has a built-in function for pretty much everything [heard it will even cook you breakfast], so if you think "wouldn't it be cool if PHP had a function to do that...", odds are it already has. Check out this example. It takes a value, gets all keys for that value if it has duplicates, unsets them all, and returns a reindexed array.



$arr contains:

Array
[
    [0] => Array
        [
            [0] => 11
            [1] => 13
        ]
?>

]

info at sabastore dot net

6 years ago

note :: array_flip is a changer for key and value and a auto unique like array_unique :

grimdestripador at hotmail dot com

8 years ago

corz at corz dot org

13 years ago

How do I flip a string in PHP?

Reversing string using strrev[]: The strrev[] function is a built-in function available in PHP and is used to reverse strings. This function takes a string as argument and returns a reversed string.

How do I flip an array in PHP?

The array_flip[] function flips/exchanges all keys with their associated values in an array.

What is array_reverse in PHP?

PHP: array_reverse[] function The array_reverse[] function is used to reverse the order of the elements in an array.

How can I reverse a string without using Strrev in PHP?

Reverse String Without using strrev[] function.

Chủ Đề