Welcome to WebmasterWorld Guest from 23.20.221.93

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)

Junior Member

10+ Year Member

joined:Jan 6, 2006
posts:104
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 15, 2004
posts:1867
votes: 0


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)

New User

5+ Year Member

joined:May 27, 2008
posts:17
votes: 0


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