From: Tontonq Tontonq on
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
....
From: Joshua Kehn on
Quickest way I can think of would be to do something like

$tmp = array();

foreach($old_array as $key => $value)
{
$tmp[$value] = $key;
}

But knowing PHP there is probably some array_reverse_keys() function.

Regards,

-Josh
____________________________________
Joshua Kehn | Josh.Kehn(a)gmail.com
http://joshuakehn.com

On Aug 31, 2010, at 11:43 AM, 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
> ....

From: Richard Quadling on
On 31 August 2010 16:43, Tontonq Tontonq <rootdot(a)gmail.com> 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
>   ....
>

$array = array_values($array);

Or, if you don't want to preserve the original array AND you want the
data sorted ...

sort($array);



--
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY
From: Ashley Sheridan on
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


From: "larry on
The fastest way is going to be array_values():

http://www.php.net/array_values

--Larry Garfield

On 8/31/10 10:43 AM, 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
> ....
>