search for in the  
<array_filterarray_intersect_assoc>
Last updated: Thu, 19 May 2005

array_flip

(PHP 4, PHP 5)

array_flip -- Exchanges all keys with their associated values in an array

Description

array array_flip ( array trans )

array_flip() returns an array in flip order, i.e. keys from trans become values and values from trans become keys.

Note that the values of trans 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 flipped.

If a value has several occurrences, the latest key will be used as its values, and all others will be lost.

array_flip() returns FALSE if it fails.

Example 1. array_flip() example

<?php
$trans
= array_flip($trans);
$original = strtr($str, $trans);
?>

Example 2. array_flip() example : collision

<?php
$trans
= array("a" => 1, "b" => 1, "c" => 2);
$trans = array_flip($trans);
print_r($trans);
?>

now $trans is:

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

See also array_values(), array_keys(), and array_reverse().



User Contributed Notes
array_flip
t0russ at gmail dot com
03-May-2005 04:00
one-liner to remove any duplicates from an array (indexes are a little messed up, but if array is huge and u only care about removing duplicates, works like a charm)

<?
$ar
=array("abba","boomer","boston","abba","bozer");
print_r(array_flip(array_flip($ar)));
?>
output:
Array
(
   [3] => abba
   [1] => boomer
   [2] => boston
   [4] => bozer
)
benles at bldigital dot com
05-Mar-2005 07:52
In case anyone wants a function that doesn't lose duplicates:

function array_invert($arr)
{
   $res = Array();
   foreach(array_keys($arr) as $key)
  {
     if (!array_key_exists($arr[$key], $res)) $res[$arr[$key]] = Array();
   array_push($res[$arr[$key]], $key);
  }
  return $res;
}
snaury at narod dot ru
23-Nov-2004 01:21
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. =^_^=
znailz at yahoo dot com
05-Aug-2003 04:42
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.

<?php
$arr
= array(11,12,13,12);        // sample array
$arr = array_flip($arr);
unset(
$arr[12]);
$arr = array(array_keys($arr));
?>

$arr contains:

<?php
Array
(
   [
0] => Array
       (
           [
0] => 11
          
[1] => 13
      
)
?>

)
rgonzalez at kidchile dot cl
06-Mar-2003 01:58
If you need traspose an array (i.e convert columns in rows) for a multidimensional array obtain from a SQL query, try this:

That is an array from arrays that represent each columns.

Array
(
   [col1] => Array
       (
           [0] => 10
           [1] => 100
           [2] => 200
           [3] => a
       )

   [col2] => Array
       (
           [0] => 1
           [1] => 3
           [2] => 2
           [3] => 5
       )
)

<?php
$arreglo_aux
= Array();
foreach(
$arreglo as $keymaster => $value )
   foreach(
$value as $key => $elemento  )
      
$arreglo_aux[$key][$keymaster] = $elemento;
?>

the results will be

Array
(
   [0] => Array
       (
           [col1] => 10
           [col2] => 1
       )

   [1] => Array
       (
           [col1] => 100
           [col2] => 3
       )

   [2] => Array
       (
           [col1] => 200
           [col2] => 2
       )

   [3] => Array
       (
           [col1] => a
           [col2] => 5
       )
)

Bye.
Rodrigo González M. - CHILE

<array_filterarray_intersect_assoc>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT