[PHP] a quick question about array keys
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
Re: [PHP] a quick question about array keys
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.k...@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 > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] a quick question about array keys
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
Re: [PHP] a quick question about array keys
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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] a quick question about array keys
On Tue, 2010-08-31 at 11:46 -0400, Joshua Kehn wrote: > 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.k...@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 > > > > That doesn't actually answer the question, it just changes the key/value pairs around. There is a built-in function for this in PHP, but it's not what the OP asked for. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] a quick question about array keys
On Aug 31, 2010, at 11:46 AM, Ashley Sheridan wrote: > On Tue, 2010-08-31 at 11:46 -0400, Joshua Kehn wrote: >> >> 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.k...@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 >> > >> >> > > That doesn't actually answer the question, it just changes the key/value > pairs around. There is a built-in function for this in PHP, but it's not what > the OP asked for. > > Thanks, > Ash > http://www.ashleysheridan.co.uk > > I misread the question as flipping array keys, my mistake. Regards, -Josh Joshua Kehn | josh.k...@gmail.com http://joshuakehn.com
Re: [PHP] a quick question about array keys
On 31 August 2010 16:43, 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 > > $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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] a quick question about array keys
Ty four your all replies i got 9 replies less than 10 minutes :) than can u answer this too my array is like that for now Array ( [300] => 300 [301] => 301 [302] => 302 [303] => 303 [304] => 304 [305] => 305 [306] => 306 [307] => 307 [308] => 308 [309] => 309 [310] => 310 [311] => 311 [312] => 312 [313] => 313 [314] => 314 [165] => 165 [166] => 166 [167] => 167 [168] => 168 [169] => 169 [170] => 170 [171] => 171 [172] => 172 [173] => 173 [201] => 201 [202] => 202 [203] => 203 [204] => 204 [205] => 205 [206] => 206 [207] => 207 [208] => 208 [209] => 209 [210] => 210 [211] => 211 [212] => 212 [213] => 213 [214] => 214 [215] => 215 [315] => 315 how can i make an array that will store values like Array ( [0] => 300-314 [1] => 165-173 ) i hope if u did understand me :D 2010/8/31 la...@garfieldtech.com > 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 >> >> >> > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >
Re: [PHP] a quick question about array keys
On Tue, 2010-08-31 at 19:06 +0300, Tontonq Tontonq wrote: > Ty four your all replies i got 9 replies less than 10 minutes :) > > than can u answer this too > my array is like that for now > Array > ( > [300] => 300 > [301] => 301 > [302] => 302 > [303] => 303 > [304] => 304 > [305] => 305 > [306] => 306 > [307] => 307 > [308] => 308 > [309] => 309 > [310] => 310 > [311] => 311 > [312] => 312 > [313] => 313 > [314] => 314 > [165] => 165 > [166] => 166 > [167] => 167 > [168] => 168 > [169] => 169 > [170] => 170 > [171] => 171 > [172] => 172 > [173] => 173 > [201] => 201 > [202] => 202 > [203] => 203 > [204] => 204 > [205] => 205 > [206] => 206 > [207] => 207 > [208] => 208 > [209] => 209 > [210] => 210 > [211] => 211 > [212] => 212 > [213] => 213 > [214] => 214 > [215] => 215 > [315] => 315 > > how can i make an array > that will store values like > Array > ( > [0] => 300-314 > [1] => 165-173 > ) > > i hope if u did understand me :D > > 2010/8/31 la...@garfieldtech.com > > > 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 > >> > >> > >> > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > I'd use a loop for something like that: $new_array = array('0-300'=>0, '301-400'=>0, '401-500'=>0, '501+'=>0); foreach($old_array as $a) { switch(true) { case $a <= 300: { $new_array['0-300']++; break; } case $a <= 400: { $new_array['301-400']++; break; } case $a <= 500: { $new_array['401-500']++; break; } default: { $new_array['501+']++; break; } } } Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] a quick question about array keys
On 31 August 2010 16:45, Ashley Sheridan wrote: > 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); sort() operates in the array. It does not return a new array, just a bool to indicate success or not. http://docs.php.net/sort -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] a quick question about array keys
Have a look at the manual, especially the function "array_values()". /frank Skickat från min iPhone. 31 aug 2010 kl. 17:43 skrev Tontonq Tontonq : > 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 > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] a quick question about array keys
On Tue, 2010-08-31 at 19:06 +0300, Tontonq Tontonq wrote: > Ty four your all replies i got 9 replies less than 10 minutes :) > > than can u answer this too > my array is like that for now > Array > ( > [300] => 300 > [301] => 301 > [302] => 302 > [303] => 303 > [304] => 304 > [305] => 305 > [306] => 306 > [307] => 307 > [308] => 308 > [309] => 309 > [310] => 310 > [311] => 311 > [312] => 312 > [313] => 313 > [314] => 314 > [165] => 165 > [166] => 166 > [167] => 167 > [168] => 168 > [169] => 169 > [170] => 170 > [171] => 171 > [172] => 172 > [173] => 173 > [201] => 201 > [202] => 202 > [203] => 203 > [204] => 204 > [205] => 205 > [206] => 206 > [207] => 207 > [208] => 208 > [209] => 209 > [210] => 210 > [211] => 211 > [212] => 212 > [213] => 213 > [214] => 214 > [215] => 215 > [315] => 315 > > how can i make an array > that will store values like > Array > ( > [0] => 300-314 > [1] => 165-173 > ) > > i hope if u did understand me :D > > 2010/8/31 la...@garfieldtech.com > > > 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 > >> > >> > >> > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > Oops, I slightly mis-read the question there in my last post. I'm not actually sure what it is you *are* after though. Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] a quick question about array keys
On Tue, 2010-08-31 at 16:58 +0100, Richard Quadling wrote: > On 31 August 2010 16:45, Ashley Sheridan wrote: > > 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); > > sort() operates in the array. It does not return a new array, just a > bool to indicate success or not. > > http://docs.php.net/sort > > > Ah, my bad! Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] a quick question about array keys
On 31 August 2010 17:06, Tontonq Tontonq wrote: > Array > ( > [300] => 300 > [301] => 301 > [302] => 302 > [303] => 303 > [304] => 304 > [305] => 305 > [306] => 306 > [307] => 307 > [308] => 308 > [309] => 309 > [310] => 310 > [311] => 311 > [312] => 312 > [313] => 313 > [314] => 314 > [165] => 165 > [166] => 166 > [167] => 167 > [168] => 168 > [169] => 169 > [170] => 170 > [171] => 171 > [172] => 172 > [173] => 173 > [201] => 201 > [202] => 202 > [203] => 203 > [204] => 204 > [205] => 205 > [206] => 206 > [207] => 207 > [208] => 208 > [209] => 209 > [210] => 210 > [211] => 211 > [212] => 212 > [213] => 213 > [214] => 214 > [215] => 215 > [315] => 315 $newData = array(); $firstValue = null; $lastValue = null; foreach($data as $value) { // New first value. if (is_null($firstValue)) { $firstValue = $value; $lastValue = null; } // New last value and is the same or 1 more. if (is_null($lastValue)) { $lastValue = $value; } // Is the value this or the next value from $lastValue else if($value == $lastValue || $value == 1 + $lastValue) { $lastValue = $value; } // We have a break; else { $newData[] = "$firstValue-$lastValue"; $firstValue = $lastValue = null; } } -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] a quick question about array keys
i think its my mistake it will begin from first value of array it will continue until if its more big than +2 and it 2010/8/31 Ashley Sheridan > On Tue, 2010-08-31 at 19:06 +0300, Tontonq Tontonq wrote: > > Ty four your all replies i got 9 replies less than 10 minutes :) > > than can u answer this too > my array is like that for now > Array > ( > [300] => 300 > [301] => 301 > [302] => 302 > [303] => 303 > [304] => 304 > [305] => 305 > [306] => 306 > [307] => 307 > [308] => 308 > [309] => 309 > [310] => 310 > [311] => 311 > [312] => 312 > [313] => 313 > [314] => 314 > [165] => 165 > [166] => 166 > [167] => 167 > [168] => 168 > [169] => 169 > [170] => 170 > [171] => 171 > [172] => 172 > [173] => 173 > [201] => 201 > [202] => 202 > [203] => 203 > [204] => 204 > [205] => 205 > [206] => 206 > [207] => 207 > [208] => 208 > [209] => 209 > [210] => 210 > [211] => 211 > [212] => 212 > [213] => 213 > [214] => 214 > [215] => 215 > [315] => 315 > > how can i make an array > that will store values like > Array > ( > [0] => 300-314 > [1] => 165-173 > ) > > i hope if u did understand me :D > > 2010/8/31 la...@garfieldtech.com > > > 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 > >> > >> > >> > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > > Oops, I slightly mis-read the question there in my last post. I'm not > actually sure what it is you *are* after though. > > > Thanks, > Ash > http://www.ashleysheridan.co.uk > > >
Re: [PHP] a quick question about array keys
On 31 August 2010 17:39, Richard Quadling wrote: > On 31 August 2010 17:06, Tontonq Tontonq wrote: >> Array >> ( >> [300] => 300 >> [301] => 301 >> ... Not sure what happened there! 300, 301 => 301, 302 => 302, 303 => 303, 304 => 304, 305 => 305, 306 => 306, 307 => 307, 308 => 308, 309 => 309, 310 => 310, 311 => 311, 312 => 312, 313 => 313, 314 => 314, 165 => 165, 166 => 166, 167 => 167, 168 => 168, 169 => 169, 170 => 170, 171 => 171, 172 => 172, 173 => 173, 201 => 201, 202 => 202, 203 => 203, 204 => 204, 205 => 205, 206 => 206, 207 => 207, 208 => 208, 209 => 209, 210 => 210, 211 => 211, 212 => 212, 213 => 213, 214 => 214, 215 => 215, 101 => 101, 315 => 315, 316 => 316, 987 => 987, ); $newData = array(); $firstValue = null; $lastValue = null; foreach($data as $value) { // New first value. if (is_null($firstValue)) { $firstValue = $value; $lastValue = null; } // New last value and is the same or 1 more or ongoing value else if ((is_null($lastValue) && $value == 1 + $firstValue) || $value == 1 + $lastValue) { $lastValue = $value; } // We have a break; else { if (!is_null($lastValue)) { $newData[] = "$firstValue-$lastValue"; } else { $newData[] = "$firstValue"; } $lastValue = null; $firstValue = $value; } } if (!is_null($firstValue)) { if (!is_null($lastValue)) { $newData[] = "$firstValue-$lastValue"; } else { $newData[] = "$firstValue"; } } print_r($newData); ?> outputs ... Array ( [0] => 300-314 [1] => 165-173 [2] => 201-215 [3] => 101 [4] => 315-316 [5] => 987 ) -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] a quick question about array keys
On 31 August 2010 17:49, Richard Quadling wrote: > On 31 August 2010 17:39, Richard Quadling wrote: >> On 31 August 2010 17:06, Tontonq Tontonq wrote: >>> Array >>> ( >>> [300] => 300 >>> [301] => 301 >>> ... If you add a ... sort($data) ... just before the foreach() loop... Array ( [0] => 101 [1] => 165-173 [2] => 201-215 [3] => 300-316 [4] => 987 ) Watch out for duplicate values. I changed 315 and 316 to 305 and 306... unsorted ... Array ( [0] => 300-314 [1] => 165-173 [2] => 201-215 [3] => 101 [4] => 305-306 [5] => 987 ) sorted ... Array ( [0] => 101 [1] => 165-173 [2] => 201-215 [3] => 300-305 [4] => 305-306 [5] => 306-314 [6] => 987 ) -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php