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 Quote 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]); } } ?> Quote 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); ?> Quote 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. Quote 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! Quote 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 Quote 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? Quote Link to comment https://forums.phpfreaks.com/topic/72517-solved-delete-from-array/#findComment-365703 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.