Forum Moderators: coopster

Message Too Old, No Replies

Maintain array value after calling unset()

         

kadnan

3:07 pm on Dec 13, 2006 (gmt 0)

10+ Year Member


Hi,

I have an array with following values:

Array (
[0] => 24.87611950862484,67.04327344894409
[1] => 24.875632837244897,67.04320907592773
[2] => 24.875282432664516,67.04310178756714
[3] => 24.874815225011737,67.04299449920654 )

now I call unset() to remove index "2" and call sort(), it just change the entire array position which is obvious. If i don't call sort then it just shows something like :

Array (
[0] => 24.87611950862484,67.04327344894409
[1] => 24.875632837244897,67.04320907592773
[3] => 24.874815225011737,67.04299449920654 )

is there anyway that i resort the keys but not values?

Thanks

whoisgregg

4:30 pm on Dec 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's probably a better way... but here's one way:

$array = array( "0" => "first", "1" => "second", "3" => "third" );
foreach($array as $arr){
$array_new[] = $arr;
}
$array = $array_new;
print_r($array);
/*
Array
(
[0] => first
[1] => second
[2] => third
)
*/

mcibor

5:07 pm on Dec 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you don't mind using more complex array functions you can always

$main_array = array(...)
unset($main_array[2]);
$main_array = array_values($main_array);

Regards
Michal