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. =^_^=