note 8717 modified in function.array-unique by irc-html

From: Date: Sat, 19 Jan 2002 21:14:57 +0000
Subject: note 8717 modified in function.array-unique by irc-html
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-25481@lists.php.net to get a copy of this message
It seems that array_unique creates an exact copy of the original array and then elimitates duplicate values. It does NOT change the "internal references" of the array. For example: <?php $test_alfa = array(); $test_alfa[0] = "aa"; $test_alfa[1] = "aa"; $test_alfa[2] = "aa"; $test_alfa[3] = "bb"; $test_alfa[4] = "aa"; $test_alfa[5] = "bb"; $test_alfa[6] = "cc"; $test_alfa[7] = "bb"; $test_beta= array_unique($test_alfa); $numValues = count($test_beta); for ($i = 0 ; $i <= 7 ; $i++) echo("test_beta[$i] = $test_beta[$i] <br>"); echo ("Number of elements in test_beta = $numValues "); ?> will give you the following output: test_beta[0] = test_beta[1] = aa test_beta[2] = test_beta[3] = test_beta[4] = test_beta[5] = bb test_beta[6] = cc test_beta[7] = Number of elements in test_beta = 3 The point is that you won't get the output you'd expect if you think that the values of the non duplicate elements are located in the first three array locations. $numValues = count($test_beta); for ($i=0;$i<=$numValues; $i++) echo("test_beta[$i] = $test_beta[$i] <br>"); echo ("Number of elements in test_beta = $numValues "); will give you: test_beta[0] = test_beta[1] = aa test_beta[2] = Number of elements in test_beta = 3 Hope that saves u some debugging time! --was-- It seems that array_unique creates an exact copy of the original array and then elimitates duplicate values. It does NOT change the "internal references" of the array. For example: &lt;?php $test_alfa = array(); $test_alfa[0] = "aa"; $test_alfa[1] = "aa"; $test_alfa[2] = "aa"; $test_alfa[3] = "bb"; $test_alfa[4] = "aa"; $test_alfa[5] = "bb"; $test_alfa[6] = "cc"; $test_alfa[7] = "bb"; $test_beta= array_unique($test_alfa); $numValues = count($test_beta); for ($i = 0 ; $i <= 7 ; $i++) echo("test_beta[$i] = $test_beta[$i] <br>"); echo ("Number of elements in test_beta = $numValues "); ?> will give you the following output: test_beta[0] = test_beta[1] = aa test_beta[2] = test_beta[3] = test_beta[4] = test_beta[5] = bb test_beta[6] = cc test_beta[7] = Number of elements in test_beta = 3 The point is that you won't get the output you'd expect if you think that the values of the non duplicate elements are located in the first three array locations. $numValues = count($test_beta); for ($i=0;$i<=$numValues; $i++) echo("test_beta[$i] = $test_beta[$i] <br>"); echo ("Number of elements in test_beta = $numValues "); will give you: test_beta[0] = test_beta[1] = aa test_beta[2] = Number of elements in test_beta = 3 Hope that saves u some debugging time! http://www.php.net/manual/en/function.array-unique.php

« previous php.notes (#25481) next »