How do I check if an array already exists?
Array Exists?
Why doesn't the following code work.
I'm trying to test if an array exists, to insert data into it, else create the array. Only each time I access the page, the array contains no data. Why is this?
<?php
session_start();
$id = $_GET['id'];
$array = $_SESSION['ids'];
if (!isset($array))
{
$array = array();
}
else
{
$array[] = 'Data';
$_SESSION['ids'] = $array;
}
echo $_SESSION['ids'];
print_r($array);
?>
It is okay to ask a follow-up question in your previous thread. So I've merged them together. No reason to have 2 threads for essentially one question.
I'm not sure why it won't work.
$array is always set because
$array = $_SESSION['ids'];
How about:
session_start();
if (!isset($_SESSION['ids']))
{
$_SESSION['ids'] = array();
}
$_SESSION['ids'][] = 'Data';
print_r($_SESSION['ids']);