php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #17750 array_unique is not returning array[0] when all values are equal
Submitted: 2002-06-13 13:35 UTC Modified: 2002-06-19 03:00 UTC
From: thiago at softexpert dot com Assigned:
Status: Closed Package: Arrays related
PHP Version: 4.2.1 OS: Windows 2000
Private report: No CVE-ID: None
 [2002-06-13 13:35 UTC] thiago at softexpert dot com
<?php
$array = array(0 => 8, 1 => "8", 2 => "8");
$array = array_unique($array);

print_r($array);
?>

The manual says:

=== MANUAL BEGIN ===
array_unique() takes input array and returns a new array without duplicate values. 

Note that keys are preserved. array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept. 

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. 

The first element will be used. 
==== MANUAL END ====

So, it should return $array[1], right ?

Its always returning the last value.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-19 03:00 UTC] venaas@php.net
I've now made it always keep the first (independent of
sorting algorithm used). In your example the result
should now be array(0 => 8). So far this is only in CVS
and latest snapshots.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 15:01:31 2024 UTC