Removing a array item and the reordering the array

I have an array like this:

array(5) { [0]=> array(8) { ["code"]=> string(4) "1001" ["desc"]=> string(28) "Martin Mac 250 : 1001 - Week" ["price"]=> string(6) "100.00" ["q"]=> string(1) "3" ["discount"]=> string(1) "0" ["bv"]=> string(3) "300" ["subtotal"]=> string(3) "300" ["con"]=> string(1) "0" } [1]=> array(8) { ["code"]=> string(4) "1000" ["desc"]=> string(24) "Shure SM58 : 1000 - Week" ["price"]=> string(5) "20.00" ["q"]=> string(1) "4" ["discount"]=> string(1) "0" ["bv"]=> string(2) "80" ["subtotal"]=> string(2) "80" ["con"]=> string(1) "0" } [2]=> array(8) { ["code"]=> string(4) "1004" ["desc"]=> string(29) "Zero88 Solution : 1004 - Week" ["price"]=> string(6) "250.00" ["q"]=> string(1) "1" ["discount"]=> string(1) "0" ["bv"]=> string(3) "250" ["subtotal"]=> string(3) "250" ["con"]=> string(1) "0" } [3]=> array(8) { ["code"]=> string(4) "1003" ["desc"]=> string(27) "Behringer X32 : 1003 - Week" ["price"]=> string(6) "100.00" ["q"]=> string(1) "1" ["discount"]=> string(1) "0" ["bv"]=> string(3) "100" ["subtotal"]=> string(3) "100" ["con"]=> string(1) "0" } [4]=> array(6) { ["code"]=> string(4) "1002" ["desc"]=> string(28) "Mackie SRM 450 : 1002 - Week" ["price"]=> string(5) "50.00" ["q"]=> string(1) "6" ["discount"]=> string(1) "0" ["con"]=> string(1) "0" } }

It comes from the contense of a form like so:

[php]











<?php if ($site_settings['showtax'] == 1){?><?php } ?>


                <tbody>
                <tr>
                		<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                <?php 
				$nett_total = 0;
				$vat_total = 0;
				$gross_total = 0;
				
				for($l=0; $l<count($item); $l++){
					
						if ($item[$l]['price']    != '0.00'){
					
					$discount = (100-$item[$l]['discount'])/100;
					
					$nett = (($item[$l]['price']*$item[$l]['q'])*$discount); 
					if ($site_settings['showtax'] == 1){$vat = $nett*($vat_at/100);}
					$gross = $vat + $nett;
				?>
                	<tr> 
                    	<td><input name="delete" type="checkbox" value="<?php echo $l; ?>[]" /></td>
                                                   
						<td><?php echo $item[$l]['code']; ?> <input type="hidden" name="item[<?php echo $l; ?>][code]" value="<?php echo $item[$l]['code']; ?>" /></td>
                        
						<td><?php echo $item[$l]['desc']; ?> <input type="hidden" name="item[<?php echo $l; ?>][desc]" value="<?php echo $item[$l]['desc']; ?>" /></td>
                        
						<td><?php echo $site_settings['currency'].number_format($item[$l]['price'],2); ?> <input type="hidden" name="item[<?php echo $l; ?>][price]" value="<?php echo $item[$l]['price']; ?>" /></td>
                        
						<td><input type="text" name="item[<?php echo $l; ?>][q]" id="q" class="inputbox 20" size="3" value="<?php echo $item[$l]['q']; ?>" /></td>
                        
                        <td><?php echo $item[$l]['discount']; ?> <input type="hidden" name="item[<?php echo $l; ?>][discount]" value="<?php echo $item[$l]['discount']; ?>" /></td>
                        
						<td><?php echo $site_settings['currency'].number_format($nett,2); ?> <input type="hidden" name="item[<?php echo $l; ?>][bv]" value="<?php echo $nett; ?>" /></td>
                        
						<?php if ($site_settings['showtax'] == 1){?><td><?php echo $site_settings['currency'].number_format($vat,2); ?> <input type="hidden" name="item[<?php echo $l; ?>][vat]" value="<?php echo $vat; ?>" /></td><?php } ?>
                        
						<td><?php echo $site_settings['currency'].number_format($gross,2); ?> <input type="hidden" name="item[<?php echo $l; ?>][subtotal]" value="<?php echo $gross; ?>" /></td>
                        	
                            <input type="hidden" name="item[<?php echo $l;?>][con]" value="<?php echo $item[$l]['con']; ?>" />
                            
					</tr>
                <?php
					$nett_total =+ $nett_total + $nett;
					if ($site_settings['showtax'] == 1){$vat_total =+ $vat_total + $vat;
					$gross_total =+ $gross_total + ($nett + $vat); } else { $gross_total =+ $gross_total + $nett;}
					
					}
				}
				?>
                </tbody>
                
                <tfoot>
                <tr>
                    	<td><input name="delete" type="submit" value="Delete Items" class="btnalt"></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    	<td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                
                <tr>
                		<td>&nbsp;</td>
                        
                    	<td><input type="text" name="item[<?php echo count($item);?>][code]" id="code" class="inputbox b50" value="" readonly/></td>
                        
                    	<td><input type="text" name="item[<?php echo count($item);?>][desc]" id="desc" class="inputbox b300" value="" /></td>
                        
                    	<td><input type="text" name="item[<?php echo count($item);?>][price]" id="price" class="inputbox b50" value="0.00" readonly/></td>
                        
                    	<td><input type="text" name="item[<?php echo count($item);?>][q]" id="q" class="inputbox b20" value="0" /></td>

                    	<td><input type="text" name="item[<?php echo count($item);?>][discount]" id="discount" class="inputbox b50" value="0" /></td>
                        
                        	<input type="hidden" name="item[<?php echo count($item);?>][con]" id="con" value="" />
                            
                    	<td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td><input name="action" type="submit" value="Update Invoice" class="btnalt"></td>
                    </tr>
                    
                    <?php if (empty($delivery) || !isset($delivery)){?>
                    <tr>
                    	<td>&nbsp;</td>
                    	<td><strong>Delivery</strong></td>
                    	<td><input type="text" name="delivery[desc]" id="del-desc" class="inputbox b300" value="" /></td>
                    	<td><input type="text" name="delivery[price]" id="del-price" class="inputbox b50" value="0.00" readonly /></td>
                        <td>&nbsp;</td>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
					<?php 
					} else {
						if ($site_settings['showtax'] == 1){$del_vat = $delivery['price']*($vat_at/100);
						$del_gross = $del_vat + $delivery['price'];} else {
						$del_gross = $delivery['price'];	
						}
					?>
                    
                    <tr>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                        <td><strong>Delivery Cost:</strong><?php echo $delivery['desc']; ?> <input type="hidden" name="delivery[desc]" value="<?php echo $delivery['desc']; ?>"  /></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td><?php echo $site_settings['currency'].number_format($delivery['price'],2); ?> <input type="hidden" name="delivery[price]" value="<?php echo $delivery['price']; ?>" /></td>
                        <?php if ($site_settings['showtax'] == 1){?><td><?php echo $site_settings['currency'].number_format($del_vat,2); ?></td><?php } ?>
                        <td><?php echo $site_settings['currency'].number_format($del_gross,2); ?></td>
                    </tr>
                    <?php 
					}
					$nett_total += $delivery['price'];
					if ($site_settings['showtax'] == 1){$vat_total += $del_vat;}
					$gross_total += $del_gross;
					?>
                     <tr class="noborder">
                     	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                        <td><strong>Nett Total:</strong></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                   		<td><?php echo $site_settings['currency'].number_format($nett_total,2); ?><input type="hidden" name="linetotal" value="<?php echo $nett_total; ?>" /></td>
                    </tr>
                    <?php if ($site_settings['showtax'] == 1){?><tr>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                        <td><strong>Vat total:</strong></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                   		<td><?php echo $site_settings['currency'].number_format($vat_total,2); ?><input type="hidden" name="totalvat" value="<?php echo $vat_total; ?>" /></td>
                    </tr><?php } ?>
                    <tr>
                    	<td>&nbsp;</td>
                    	<td>&nbsp;</td>
                        <td><strong>Grand total:</strong></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                   		<td><?php echo $site_settings['currency'].number_format($gross_total,2); ?> <input type="hidden" name="grandtotal" value="<?php echo $gross_total; ?>" /></td>
                    </tr>
                    
                </tfoot>
                
                </table>[/php]

I have made this delete syntax to go along with the removal of a line(s) that are ticked:

[php]if(isset($_POST[‘delete’]) && $_POST[‘action’] == “Delete Items”){
$delivery = $_POST[‘delivery’];

	if ($delivery['price'] == '0.00' && $delivery['desc'] == '') {
		$delivery = NULL; 
	}

$item = $_POST[‘item’];

$customername = $_POST[‘customer’];
$contactname = $_POST[‘contact’];
$customerid = $_POST[‘customerid’];
$address1 = $_POST[‘address1’];
$address2 = $_POST[‘address2’];
$address3 = $_POST[‘address3’];
$postcode = $_POST[‘postcode’];
$jobid = $_POST[‘jid’];
$from = $_POST[‘from’];
$to = $_POST[‘to’];
$notes = $_POST[‘notes’];

foreach($delete as $d){

unset($item[$d]);
}

array_values($item);

}[/php]

Problem is that it doesn’t work and clears everything! If you want the full file to help PM me an email address and I will send it over as I can’t upload it here. [/code]

  Product ID Description Cost Qty Discount (%) Nett VAT Gross

I don’t see where $delete array is defined?

Sorry:

[php]
if(isset($_POST[‘delete’]) && $_POST[‘action’] == “Delete Items”){
$delivery = $_POST[‘delivery’];

	if ($delivery['price'] == '0.00' && $delivery['desc'] == '') {
		$delivery = NULL; 
	}

$item = $_POST[‘item’];

$customername = $_POST[‘customer’];
$contactname = $_POST[‘contact’];
$customerid = $_POST[‘customerid’];
$address1 = $_POST[‘address1’];
$address2 = $_POST[‘address2’];
$address3 = $_POST[‘address3’];
$postcode = $_POST[‘postcode’];
$jobid = $_POST[‘jid’];
$from = $_POST[‘from’];
$to = $_POST[‘to’];
$notes = $_POST[‘notes’];

$delete = $_POST[‘delete’];

foreach($delete as $d){

unset($item[$d]);
}

array_values($item);

}[/php]

You’re not creating an array of items to delete. You are creating individual arrays for each key. e.g. 0[], 1[], 2[], etc…

Here is an example:

[php]

<?php $items = array( 0 => 'key 0', 1 => 'key 1', 2 => 'key 2' ); if (isset($_POST['delete'])) { $keys = array_map('intval', $_POST['delete']); foreach($keys as $key) { unset($items[$key]); } var_dump($items); } ?> key 0:
key 1:
key 2:
[/php]

Instead of using arrays i just put the items direct into a DB and deleted them from there. I find that it will be more reliable.

Thanks for the help though. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service