如何在php中添加,删除和删除所有会话数组值?

In this code, I can't fetch id value. How can I give a number instead of if(strlen) ? And how can I give remove one by one item ?

<?Php
    @$product=$_POST['product'];

    if (strlen($product)>3) {
        array_push($_SESSION['cart'],$product); // Items added to cart
    }
?>

<?Php
    if (isset($_SESSION['cart'])) {

        echo " <br><a href=cart-remove-all.php>Remove all</a><br>";
        while (list ($key, $val) = each ($_SESSION['cart'])) { 
            echo "$key -> $val <br>"; 
        } else {
            echo " Session Cart is not created. Visit <a href=cart.php>cart.php</a> page 
to create the array and add products to it. ";
        }
?>

<form method="post">  
    <input type="hidden" name="product" value="<?php echo $row['jobtitle'];?>">
    <input type="submit" style="float:right;font-size:15px;font-weight:600;" value="+ Add to Short List">
</form>

to remove all, just do :

unset($_SESSION['cart']);

and, rather than using a while loop, you can use foreach loop.you can remove the item by using its array index in $_SESSION['cart'];

If I have understood your question clearly, you can use

if(isset($_SESSION['cart'][0]))
unset($_SESSION['cart'][0]);

Likewise, you can change 0,1,2...n to unset the nth record. BTW, once you unset one record the number of records in the array will become n-1 from n. if you keep doing the above code repeatedly until the count of the records become 0 also will work

Use foreach

foreach($_SESSION['cart'] as $key=>$value){
    if($ke==="yourkey"){
        unset($_SESSION['cart'][$key]); 
    }

}

yourkey is the key, you want to remove.