How to Remove a Variable from a PHP Session Array

How to remove a variable from a PHP session array


if (isset($_POST['remove'])) {
$key=array_search($_GET['name'],$_SESSION['name']);
if($key!==false)
unset($_SESSION['name'][$key]);
$_SESSION["name"] = array_values($_SESSION["name"]);
}

Since $_SESSION['name'] is an array, you need to find the array key that points at the name value you're interested in. The last line rearranges the index of the array for the next use.

How to remove a single element from a PHP session array?

The easiest way is to get the value, remove the item, and set the session variable again.

$data = $_SESSION['array'];    // Get the value
unset($data[1]); // Remove an item (hardcoded the second here)
$_SESSION['array'] = $data; // Set the session value with the new array

Update:

Or like @Qirel said, you can unset the item directly if you know the number.

unset($_SESSION['array'][1]);

Update 2

If you want to remove the element by its value, you can use array_search to find the key of this element. Note that if there are to elements with this value, only the first will be removed.

$value_to_delete = '13/02/2017';
if (($key = array_search($value_to_delete, $_SESSION['array'])) !== false)
unset($_SESSION['array'][$key]);

Removing a certain number out of a session array in PHP

Why don't you try array_search() and unset()? It's easier, have a look at the code below and adapt it to your situation:

$array = [1, 5, 6, 12];
$wantToRemove = 5;

$key = array_search($wantToRemove, $array);
unset($array[$key]);

var_dump($array);

PHP Unset Session Variable

You can unset session variable using:

  1. session_unset - Frees all session variables (It is equal to using: $_SESSION = array(); for older deprecated code)
  2. unset($_SESSION['Products']); - Unset only Products index in session variable. (Remember: You have to use like a function, not as you used)
  3. session_destroy — Destroys all data registered to a session

To know the difference between using session_unset and session_destroy, read this SO answer. That helps.

How to remove some session array segment after click button in codeigniter

Try this, hope it'll help

if($deleted_plan)
{
foreach($final_session as $k => $session)
{
if(isset($session[$deleted_plan]))
{
unset($final_session[$k][$deleted_plan]);
}
}
$final_session = array_values($final_session);
$this->session->set_userdata('payment_plans', $final_session);
}

How to remove an item from session array in laravel

You AFAIR have to firstly retrieve whole array, edit it and then set it again. If you want to delete by product ID, which is as I assume an array value, you can use this: PHP array delete by value (not key)

$products = session()->pull('products', []); // Second argument is a default value
if(($key = array_search($idToDelete, $products)) !== false) {
unset($products[$key]);
}
session()->put('products', $products);

Misunderstood question

Session::pull takes first parameter as the item do delete and second as the default value to return. You have mistaken the order of arguments. Try:

session()->pull('products'); // You can specify second argument if you need default value

As I can see in source, Session::forget expects string or array, so you should specify only the first parameter:

session()->forget('products');


Related Topics



Leave a reply



Submit