Re: a quick question about array keys

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

 



On Tue, 2010-08-31 at 18:43 +0300, Tontonq Tontonq wrote:

> a quick question
> lets say i have an array like that
> 
> 
> Array
> (
> [300] => 300
> [301] => 301
> [302] => 302
> [303] => 303
> [304] => 304
> [305] => 305
> [306] => 306
> [307] => 307
> [308] => 308
> ...
> how can i change keys to 0,1,2,3,.. by faster way
> (it should like that) >
> Array
> (
>   [0] => 300
>   [1] => 301
>   [2] => 302
>   [3] => 303
>    ....


There are two ways I see to do it. You can iterate the array and create
a copy, assigning elements dynamic values:

$new_array = array();
foreach($array as $a)
{
    $new_array[] = $a;
}

or use a sorting function on it that doesn't preserve the keys (as in
your example all the values in the array were in numerical order.

$new_array = sort($array);

Having said that, if the key isn't important, and it doesn't seem to be
if you want to change it, then why not use a foreach and leave the key
as it is?

Thanks,
Ash
http://www.ashleysheridan.co.uk



[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