php_novice2007 Posted August 25, 2007 Share Posted August 25, 2007 Hi all, Say I've got a php array of 10 elements, and I want to delete the 2nd, 5th, and the 8th element. I can't seem to find a remove function in the manual.. can anyone help? thanks! Link to comment https://forums.phpfreaks.com/topic/66618-php-arrays-deleting-elements/ Share on other sites More sharing options...
vijayfreaks Posted August 25, 2007 Share Posted August 25, 2007 Hi.. see unset() function you can do this via; unset($arr[2]); unset($arr[5]); unset($arr[8]); will delete the 2nd, 5th, and the 8th element. -vijay Link to comment https://forums.phpfreaks.com/topic/66618-php-arrays-deleting-elements/#findComment-333755 Share on other sites More sharing options...
php_novice2007 Posted August 25, 2007 Author Share Posted August 25, 2007 When I do unset($arr[2]), would all the elements move up and the 5th element becomes the 4th element and then I will have to keep track of the size of the array to unset the right indexes? Link to comment https://forums.phpfreaks.com/topic/66618-php-arrays-deleting-elements/#findComment-333831 Share on other sites More sharing options...
chocopi Posted August 25, 2007 Share Posted August 25, 2007 well couldn't you just set them to nothing $arr[2] = ''; $arr[5] = ''; $arr[8] = ''; or $arr[2] = array(); $arr[5] = array(); $arr[8] = array(); Is that what you wanted ? ~ Chocopi Link to comment https://forums.phpfreaks.com/topic/66618-php-arrays-deleting-elements/#findComment-333834 Share on other sites More sharing options...
sasa Posted August 25, 2007 Share Posted August 25, 2007 try and see <?php $a = array('aa','bb','cc','dd','ee','ff','gg','hh','ii','jj','kk'); unset($a[2]); echo "\n<br />\n"; print_r($a); //after you can rebuild keys $a = array_values($a); echo "\n<br />\n"; print_r($a); ?> Link to comment https://forums.phpfreaks.com/topic/66618-php-arrays-deleting-elements/#findComment-333933 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.