justinh Posted February 3, 2009 Share Posted February 3, 2009 i have this array array(7) { [0]=> string(20) "UNIT QTY $ AMOUNT" [1]=> string(18) "1c 18 0.18+" [2]=> string(18) "1c 18 0.18+" [3]=> string(19) "TOTAL 0.36+" [4]=> string(19) "TOTAL 0.00+" [5]=> string(0) "" [6]=> string(0) "" } I looked all over PHP.net for a way to delete an element in an array, and can't seem to find it. Does anyone have an idea on how I would go about this? This is what I have so far <?php $pieces = explode("<br>", $bankinfo); foreach($pieces as $value){ if($value == "" || $value == "UNIT QTY $ AMOUNT"){ //toss it out } echo $value . "<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/143606-array-delete/ Share on other sites More sharing options...
Vebut Posted February 3, 2009 Share Posted February 3, 2009 http://php.net/unset Link to comment https://forums.phpfreaks.com/topic/143606-array-delete/#findComment-753463 Share on other sites More sharing options...
justinh Posted February 3, 2009 Author Share Posted February 3, 2009 suwheet this worked <?php $pieces = explode("<br>", $bankinfo); foreach($pieces as $value){ if($value == "" || $value == "UNIT QTY $ AMOUNT"){ unset($value); } echo $value . "<br />"; } ?> But... It's not unsetting the UNIT QTY & AMOUNT element, any idea why? Link to comment https://forums.phpfreaks.com/topic/143606-array-delete/#findComment-753466 Share on other sites More sharing options...
Vebut Posted February 3, 2009 Share Posted February 3, 2009 I don't get it, why use explode if you already have an array? Link to comment https://forums.phpfreaks.com/topic/143606-array-delete/#findComment-753481 Share on other sites More sharing options...
sasa Posted February 3, 2009 Share Posted February 3, 2009 <?php $pieces = explode("<br>", $bankinfo); foreach($pieces as $key => $value){ if($value == "" || $value == "UNIT QTY $ AMOUNT"){ unset($pieces[$key]); } echo $value . "<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/143606-array-delete/#findComment-753512 Share on other sites More sharing options...
printf Posted February 3, 2009 Share Posted February 3, 2009 You can do something like this. Internally PHP is faster than any external loop with a single or more condition(s)! <?php $original = array ( 0 => 'UNIT QTY $ AMOUNT', 1 => 'junk', 2 => '' ); $original = array_diff ( $original, array ( '', 'UNIT QTY $ AMOUNT' ) ); print_r ( $original ); ?> Link to comment https://forums.phpfreaks.com/topic/143606-array-delete/#findComment-753519 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.