simon551 Posted October 9, 2007 Share Posted October 9, 2007 let's say I have an array $letters(a,b,c,d,e,f) is there a function that will remove 'd' from the middle. I'm looking for something like array_delete($letters,'d'); Thanks in advance. -s Link to comment https://forums.phpfreaks.com/topic/72517-solved-delete-from-array/ Share on other sites More sharing options...
MmmVomit Posted October 9, 2007 Share Posted October 9, 2007 <?php $alphabet = Array('a', 'b', 'c', 'd', 'e', 'f'); foreach($alphabet as $k => $v) { if($v == 'd') { unset($alphabet[$k]); } } ?> Link to comment https://forums.phpfreaks.com/topic/72517-solved-delete-from-array/#findComment-365660 Share on other sites More sharing options...
sasa Posted October 9, 2007 Share Posted October 9, 2007 try <?php function array_delete(&$arr, $value) { $keys = array_keys($arr, $value); foreach ($keys as $key) unset ($arr[$key]); } $letters = array (a,b,c,d,e,f); array_delete($letters, 'd'); print_r($letters); ?> Link to comment https://forums.phpfreaks.com/topic/72517-solved-delete-from-array/#findComment-365661 Share on other sites More sharing options...
Orio Posted October 9, 2007 Share Posted October 9, 2007 An alternative way, although I doubt if it's better lol: <?php $arr = array('a','b','c','d'); $search = 'd'; $bad = array_keys($arr, $search); for($i=0; $i<count($bad); unset($arr[$bad[$i++]])); ?> Orio. Link to comment https://forums.phpfreaks.com/topic/72517-solved-delete-from-array/#findComment-365667 Share on other sites More sharing options...
simon551 Posted October 9, 2007 Author Share Posted October 9, 2007 Thanks much to you all! Link to comment https://forums.phpfreaks.com/topic/72517-solved-delete-from-array/#findComment-365668 Share on other sites More sharing options...
kenrbnsn Posted October 9, 2007 Share Posted October 9, 2007 I know you've said "solved" on this, but here's one more way ... <?php $t = array('a1','v','2','x5','z6','v'=>'7'); $nt = array_delete('z6',$t); echo '<pre> ' . print_r($t,true) . '</pre>'; echo '<pre> ' . print_r($nt,true) . '</pre>'; function array_delete($x, $ary) { if (!is_array($x)) $x = array($x); return(array_diff($ary,$x)); } ?> Ken Link to comment https://forums.phpfreaks.com/topic/72517-solved-delete-from-array/#findComment-365700 Share on other sites More sharing options...
MmmVomit Posted October 9, 2007 Share Posted October 9, 2007 Anyone up for a game of golf? Link to comment https://forums.phpfreaks.com/topic/72517-solved-delete-from-array/#findComment-365703 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.