Re: note 8363 added to array_unique
| From: | J. M. Castagnetto | Date: | Thu, 07 Sep 2000 22:02:16 +0000 |
| Subject: | Re: note 8363 added to array_unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-4461@lists.php.net to get a copy of this message | ||
It is your logic what is wrong. You were expecting
that after you unique the array, the index magically
will be reset to be consecutive, that is not what
happens because it will fail for associative arrays,
try instead:
<?
$tarray =
array('General','Social','Social','Social','Social','Social','Sports','Sports','Sports','Sports','Topic','Topic','Topic','Topic','Topic','Topic','Misc','Misc','Misc','Misc','Misc','Misc','Misc','Misc');
echo "initial array\n";
print_r($tarray);
$tarray = array_unique($tarray);
echo "after array_unique\n";
print_r($tarray);
?>
And if you want to step through and array, there are 3
ways of doing it, using next() and companions in the
for loop, using while(list($k,$v) = each($tarray)) or
using foreach($tarray as $k=>$v). Take your pick.
Your note will be removed from the online manual. BTW,
look for support on the PHP general list, not on the
annotations to the manual.
--- rredmon@spiggle.com wrote:
> bug in array_unique, ran this code using version
> 4.0.1pl2:
> <?
> $tarray =
>
array('General','Social','Social','Social','Social','Social','Sports','Sports','Sports','Sports','Topic','Topic','Topic','Topic','Topic','Topic','Misc','Misc','Misc','Misc','Misc','Misc','Misc','Misc');
> echo "\n*".join(':',$tarray)."*\n";
> $tarray = array_unique($tarray);
> echo "\n*".join(':',$tarray)."*\n";
> echo "\nsize=".sizeof($tarray)."\n";
>
> for ($r = 0; $r < sizeof($tarray); $r++)
> echo "\n*".$tarray[$r]."*";
> echo "\n*".join(':',$tarray)."*\n";
>
> echo "\n";
> ?>
> got this output
>
>
>
*General:Social:Social:Social:Social:Social:Sports:Sports:Sports:Sports:Topic:Topic:Topic:Topic:Topic:Topic:Misc:Misc:Misc:Misc:Misc:Misc:Misc:Misc*
>
> *General:Social:Sports:Topic:Misc*
>
> size=5
>
> *General*
> *Social*
> **
> **
> **
> *General:Social:Sports:Topic:Misc*
>
> WHAT Gives here?, Why does join("",$tarray) give the
> expected output while $tarray[2] does not?
>
>
> http://www.php.net/manual/function.array-unique.php
>
>
>
> --
> PHP Notes Mailing List (http://www.php.net/)
> To unsubscribe, e-mail:
> php-notes-unsubscribe@lists.php.net
> For additional commands, e-mail:
> php-notes-help@lists.php.net
> To contact the list administrators, e-mail:
> php-list-admin@lists.php.net
>
=====
--- Jesus M. Castagnetto (jcastagnetto@yahoo.com)
__________________________________________________
Do You Yahoo!?
Yahoo! Mail - Free email you can access from anywhere!
http://mail.yahoo.com/