Jump to content

array delete?


justinh

Recommended Posts

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
Share on other sites

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
Share on other sites

<?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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.