Doc #62822 [Opn->Nab]: array_unique() - Confusing example

From: Date: Thu, 23 Aug 2012 16:23:03 +0000
Subject: Doc #62822 [Opn->Nab]: array_unique() - Confusing example
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-8757@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=62822&edit=1 ID: 62822 Updated by: googleguy@php.net Reported by: danielklein at airpost dot net Summary: array_unique() - Confusing example -Status: Open +Status: Not a bug Type: Documentation Problem Package: Documentation problem PHP Version: Irrelevant Block user comment: N Private report: N New Comment: It's neither the first nor last value that determines the type of anything here. PHP determines type based on context of the value. See http://php.net/language.types.type-juggling for details. However, types are not being determined here. We're just discovering values and whichever value happens to be discovered first in the array -- that array key will be preserved. The implementation of array_unique() is such that it will "keep the first key encountered for every value". This is already noted in the documentation in the Description section of array_unique at the top. This is also why the resulting output from var_dump() in the examples shows that key's 0 and 2 are the remaining results. array(2) { [0] => int(4) [2] => string(1) "3" } 0 being the offset of the first element in the array, and 2 being the offset of the third element in the array. Previous Comments: ------------------------------------------------------------------------ [2012-08-15 00:27:39] danielklein at airpost dot net Description: ------------ --- From manual page: http://www.php.net/function.array-unique#refsect1-function.array-unique-examples --- In the example code int(4) and string("3") are both the first and last versions of those values encountered! <?php $input = array(4, "4", "3", 4, 3, "3"); ^ ^^^ ^ ^^^ $result = array_unique($input); var_dump($result); ?> It would be better written as <?php $input = array(4, "4", "3", "4", 3, 3); $result = array_unique($input); var_dump($result); ?> or something else that shows it's the _first_ value that determines the type, not the last. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=62822&edit=1

« previous php.doc.bugs (#8757) next »