How to remove a variable from a PHP session array

0 votes

I have PHP code that is used to add variables to a session:

<?php
    session_start();
    if(isset($_GET['name']))
    {
        $name = isset($_SESSION['name']) ? $_SESSION['name'] : array();
        $name[] = $_GET['name'];
        $_SESSION['name'] = $name;
    }
    if (isset($_POST['remove']))
    {
        unset($_SESSION['name']);
    }
?>
<pre>  <?php print_r($_SESSION); ?>  </pre>

<form name="input" action="index.php?name=<?php echo $list ?>" method="post">
  <input type="submit" name ="add"value="Add" />
</form>

<form name="input" action="index.php?name=<?php echo $list2 ?>" method="post">
  <input type="submit" name="remove" value="Remove" />
</form>

I want to remove the variable that is shown in $list2 from the session array when the user chooses 'Remove'.

But when I unset, ALL the variables in the array are deleted.

How I can delete just one variable?

Nov 8, 2020 in PHP by kartik
• 37,520 points
4,882 views

1 answer to this question.

0 votes

Hello @kartik,

Try:

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.

answered Nov 8, 2020 by Niroj
• 82,840 points

Related Questions In PHP

0 votes
1 answer

How to load return array from a PHP file?

Hii @kartik, When an included file returns something, ...READ MORE

answered Nov 10, 2020 in PHP by Niroj
• 82,840 points
3,213 views
0 votes
1 answer

How to include a PHP variable inside a MySQL statement?

Hii, You can try this: $query="SELECT * FROM CountryInfo ...READ MORE

answered Apr 9, 2020 in PHP by Niroj
• 82,840 points
17,075 views
0 votes
1 answer

How to call a php function from ajax?

Hello @kartik, You can't call a PHP function ...READ MORE

answered Jun 16, 2020 in PHP by Niroj
• 82,840 points
12,781 views
0 votes
1 answer

How to resolve the problem of losing a session after a redirect in PHP?

Hello @kartik, Carry out these usual checks: Make sure session_start(); is ...READ MORE

answered Aug 24, 2020 in PHP by Niroj
• 82,840 points
35,172 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,095 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
3,019 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,951 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,897 views
0 votes
1 answer

How to discard a variable from a PHP session array

Hello, Try this: if (isset($_POST['remove'])) { ...READ MORE

answered Nov 13, 2020 in PHP by Niroj
• 82,840 points
794 views
0 votes
1 answer

How to remove duplicate values from an array in PHP?

Hello @kartik, Use array_unique(): Example: $array = array(1, 2, 2, 3); $array ...READ MORE

answered Sep 15, 2020 in PHP by Niroj
• 82,840 points
2,751 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP