Welcome to WebmasterWorld Guest from 23.22.79.235

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

how to delete array element with shifting the indexes ?

   
9:24 am on Apr 22, 2008 (gmt 0)

5+ Year Member



hello forums!
Can anybody suggest in the following case:
How to remove an element from an index array and reseting its indexes?
Example:
[PHP]$array = array('x', 'y', 'z',..);
unset($array[1]);
print_r($array); // results: 0 => x, 2 => z ,which doesn't reset the indexes
// I would like to have the result as : 0 => x, 1 => z , ie rearraning the indexes[/PHP]
How to accomplish this ?
Thanks in advance for the suggestions.
10:12 am on Apr 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can add this line right after unset
$b = array_values($a);

...and test:
print_r($b)

More about it here [php.net]

8:42 am on May 27, 2008 (gmt 0)

5+ Year Member



array_splice($x,1,1);
print_r($x);