Re: array problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



You're wrong, he isn't using an associative array, since the keys used are
only integers.

array(10,10,40,30,30,10);
and
array(0=>10,1=>10,2=>40,3=>30,4=>30,5=>10);
create the same array.

The problem is that array_unique preserves keys (read the manual!!!)
If you don't want this, use array_values() to turn the array into a more
traditional array:

$a = array_values(array_unique($a));


2006/4/22, Brian V Bonini <b-bonini@xxxxxxx>:
>
> On Sat, 2006-04-22 at 07:09, suresh kumar wrote:
> >  sorry.earlier i mistyped some values.
> >
> >   I am facing one project in my project .
> >
> >   this is my  code:
> >
> >   a=array(0=>10,1=>10,2=>40,3=>30,4=>30,5=>10);
> >   b=array();
> >   b=array_unique($a);
> >   print_r($b);
> >   o/p  getting from above code is  b[0]=10,b[2]=40,b[3]=30,b[5]=10;
> >
> >   but i want the o/p be b[0]=10,b[1]=40,b[2]=30,b[3]=10;
>
>
> That will return:
>
> Array
> (
>     [0] => 10
>     [2] => 40
>     [3] => 30
> )
>
> If you want:
>
> Array
> (
>     [0] => 10
>     [1] => 40
>     [2] => 30
> )
>
>
> Don't use an associative array for $a
> $a=array(10,10,40,30,30,10);
>
> Or iterate through $a to re-sequence the index in $b.
>
> $a=array(0=>10,1=>10,2=>40,3=>30,4=>30,5=>10);
> $a=array_unique($a);
>
> foreach($a as $v) {
>         $b[] = $v;
> }
>
> print_r($b);
>
> --
>
> s/:-[(/]/:-)/g
>
>
> Brian        GnuPG -> KeyID: 0x04A4F0DC | Key Server: pgp.mit.edu
> ======================================================================
> gpg --keyserver pgp.mit.edu --recv-keys 04A4F0DC
> Key Info: http://gfx-design.com/keys
> Linux Registered User #339825 at http://counter.li.org
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux