Pergunta

I created a session array using

$_SESSION['create'] = array();

then filled out the array using this,

$_SESSION['create']['new-campaign-name']    = $_POST['new-campaign-name'];
$_SESSION['create']['new-campaign-type']    = $_POST['new-campaign-type'];
$_SESSION['create']['new-campaign-country'] = $_POST['new-campaign-country'];
$_SESSION['create']['new-campaign-list']    = $_POST['new-campaign-contact'];
$_SESSION['create']['new-campaign-des']     = $_POST['new-campaign-des'];

but i would want to delete all of the content of the session array.

i tried this

unset($_SESSION['create']);

but it seems to be not working and all of the values are still on the session data, is there a way to reset the contents of my session array?

Foi útil?

Solução 2

Basically you want to set the create part of the session empty again. You do this by using $_SESSION['create'] = array(); again, as you did to initialize it.

Outras dicas

You'll want to use the following built-in function to do so

session_unset();

session_unset on PHP.net

Here are some tests that I ran using your existing session variables, including some of my own.

Consult the comments throughout the script.

<?php
session_start();
$_SESSION['create'] = array();

// this one can be used also
// $_SESSION=array();

/*
$_SESSION['create']['new-campaign-name']    = $_POST['new-campaign-name'];
$_SESSION['create']['new-campaign-type']    = $_POST['new-campaign-type'];
$_SESSION['create']['new-campaign-country'] = $_POST['new-campaign-country'];
$_SESSION['create']['new-campaign-list']    = $_POST['new-campaign-contact'];
$_SESSION['create']['new-campaign-des']     = $_POST['new-campaign-des'];

$_SESSION['create_2']['new-campaign-des_2']     = $_POST['new-campaign-des_2'];

*/

// my own test without POST variables
$_SESSION['create']['new-campaign-name']    = 'new-campaign-name';
$_SESSION['create']['new-campaign-type']    = 'new-campaign-type';
$_SESSION['create']['new-campaign-country'] = 'new-campaign-country';
$_SESSION['create']['new-campaign-list']    = 'new-campaign-contact';
$_SESSION['create']['new-campaign-des']     = 'new-campaign-des';

$_SESSION['create_2']['new-campaign-des_2']     = 'new-campaign-des_2';

// echo "Session variables above this";

echo $_SESSION['create']; // this will echo Array

echo "<br>";

// this will unset all in $_SESSION['create'] group
unset($_SESSION['create']);

echo "<br>";

var_dump($_SESSION);

echo "<hr>";

// this will echo NULL
var_dump($_SESSION['create']);

echo "<hr>";

// this will echo Array even after unsetting $_SESSION['create']
echo $_SESSION['create_2'];

echo "<hr>";

// this will echo/dump
// array(1) { ["new-campaign-des_2"]=> string(18) "new-campaign-des_2" } 
var_dump($_SESSION['create_2']);

echo "<hr>";

// this will echo/dump
// array(1) { ["create_2"]=> array(1) { ["new-campaign-des_2"]=> string(18) "new-campaign-des_2" } }
// but not anything in the ['create'] group
var_dump($_SESSION);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top