Prev: Caution Fraud Alert about Antony Papamichail
Next: Removing link on the fly, but leave link text
From: Tontonq Tontonq on 31 Aug 2010 11:43 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 31 Aug 2010 11:46 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 31 Aug 2010 11:46 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 31 Aug 2010 11:45 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 31 Aug 2010 11:47 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 > .... >
|
Next
|
Last
Pages: 1 2 3 4 Prev: Caution Fraud Alert about Antony Papamichail Next: Removing link on the fly, but leave link text |