如何按键号删除数组的值。 它甚至可能吗?

I have come seeking the wisdom of all of you wise and smart people! After looking all day yesterday and a few hours today i've given up. I've tried many things but nothing has worked and i've come to the conclusion this problem is beyond my skills. My php skills are pretty basic and not very advanced.

I've created a simple shopping cart with $_SESSION. The checkout page provides the order information to a cart page that i have working nicely based on this tutorial http://keito.me/dynamicweb/11/

The items are added to the car through a foreach loop.

My problem is i am not able to delete the items added to the cart. I would like to have the Remove button to remove that specific item and not the whole order.

Is there a way to delete array values by key? since each item has an unique key number.. so far i've found no answers. All i've been able to find has been the unset. However i can't find a way to unset the array from a specific key. Here is the code that displays and loops the items.

So my questions is: Is it possible and how can i delete an array based on key number? Or just simply put, how can i delete each item added to the table.

if (!isset($_SESSION['cart']) || (count($_SESSION['cart']) == 0)) {
  echo '<p>Your cart is empty.</p>';
} else {

echo '<table id="myTable"  width="100%" border="1" cellpadding="3" cellspacing="1">
<tr><th>Door Model</th><th>Door Size</th><th>Door Information</th><th>Quantity</th>      <th>Subtotal</th></tr>';
  $total = 0;

  $bananas = $_SESSION['cart'];
    foreach($bananas as $key =>$item) {

 echo "<tr><td align='center' width='10%' valign='top'>{$item['1']}
    </td><td width='10%'  align='center' valign='top'>{$item['2']}</td>
    <td valign='top'>{$item['4']}
    <form method='post' action='{$_SERVER['PHP_SELF']}'>
    <p>Key = $key  Item = $item</p>
    <input name='submited$key' type='submit' value='Remove' />
    </form>
    <p><a href='unset.php'>Remove</a>    <a href='#'>Edit</a></p></td>
    <td align='center' valign='top' cellpadding='3' width='5%'>{$item['3']}</td>
    <td width='10%'align='center' valign='top'>$".number_format($item['6'] * $item['3'],2)."</td></tr>";

    $total += ($item['6'] * $item['1']);

   }

echo "<tr><th colspan='5' scope='row'><p style='float: right'><strong>Grand total: </strong>\$".number_format($total, 2);"</p></th></tr>";      
echo '</table>';


  }
?>

A simple unset($array[$key]) should do the trick.

It will not work with pass-by-reference values, however.

use unsset($array[$index]) this will remove index from array.

if you need remove from $bananas array:

$bananas = $_SESSION['cart'];

foreach($bananas as $key => $item) {
    // do something with $bananas[$key]
    unset($bananas[$key]);
}

if you need remove from session:

$bananas = $_SESSION['cart'];

foreach($bananas as $key => $item) {
    // do something with $bananas[$key]
    unset($_SESSION['cart'][$key]);
}

You can use ArrayIterator for this:-

$array = array('zero', 'one','two','three','four','five','six','seven');
$iterator = new \ArrayIterator($array);
var_dump($iterator);
$iterator->offsetUnset('3');
var_dump($iterator);

Output:-

object(ArrayIterator)[1]
  string 'zero' (length=4)
  string 'one' (length=3)
  string 'two' (length=3)
  string 'three' (length=5)
  string 'four' (length=4)
  string 'five' (length=4)
  string 'six' (length=3)
  string 'seven' (length=5)

object(ArrayIterator)[1]
  string 'zero' (length=4)
  string 'one' (length=3)
  string 'two' (length=3)
  string 'four' (length=4)
  string 'five' (length=4)
  string 'six' (length=3)
  string 'seven' (length=5)

As you can see the element with index 3 has been removed.

First, if I were you I wouldn't use PHP for that, PHP will require a reload to work, though you can use a very simple JQuery string to get the job done for you with a nice graphical effect and without the need for page reloading,
Just declare a specific an ID (or class) for the "removal button", then target that ID using this simple script :

<script>
$(function () {
$('a#delete_row').live('click', function() {
    $(this).parent().parent().parent().animate({ backgroundColor: "#ffcece" }, 'fast' , function() {
        $(this).fadeOut( function() {
                $(this).remove();
        });
    });
  });
});
</script>

This will remove the table item with nice graphical declaration, and pulls it out of the DOM tree so that it's actually deleted "not just hidden" if you post your form.

Note: I used the (Live) selector to make it dynamically update whenever the cart got bigger,
You'll need other JQuery stuff to "deduce" this item price from the grand total after removal ...