physaux Posted November 4, 2009 Share Posted November 4, 2009 Here is a theoretical array: myarray[0] = "word"; myarray[1] = "bottle"; myarray[2] = "screen"; myarray[3] = "php"; myarray[4] = "wow!"; ... How could I remove the array element with the value, (for example), "php"? I want to be left without that entry, and the index missing a number after doesn't bother me. myarray[0] = "word"; myarray[1] = "bottle"; myarray[2] = "screen"; myarray[4] = "wow!"; ... Any suggestions? Thanks! Link to comment https://forums.phpfreaks.com/topic/180324-solved-how-can-i-remove-an-element-of-an-array-without-knowing-its-index-just-value/ Share on other sites More sharing options...
simshaun Posted November 4, 2009 Share Posted November 4, 2009 array_search() to get the key. Think you know the rest.. (if not, take a look at unset().) Link to comment https://forums.phpfreaks.com/topic/180324-solved-how-can-i-remove-an-element-of-an-array-without-knowing-its-index-just-value/#findComment-951231 Share on other sites More sharing options...
mrMarcus Posted November 4, 2009 Share Posted November 4, 2009 <?php $myarray = array ('asdf','php','qwer'); foreach ($myarray as $k => $v) { if ($v == 'php') { unset ($k); } else { $newarray[] = $v; } } print_r ($newarray); ?> Link to comment https://forums.phpfreaks.com/topic/180324-solved-how-can-i-remove-an-element-of-an-array-without-knowing-its-index-just-value/#findComment-951234 Share on other sites More sharing options...
physaux Posted November 4, 2009 Author Share Posted November 4, 2009 got it thanks Link to comment https://forums.phpfreaks.com/topic/180324-solved-how-can-i-remove-an-element-of-an-array-without-knowing-its-index-just-value/#findComment-951235 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.