How to store a single array value into a session variable? [closed]
-
29-09-2020 - |
Question
I have
Array ( [0] => cid [1] => TN201501HP )
Now I want to store "TN201501HP" into some session variable. How to store this value to a session variable?
Solution
When you store array(object) in variable use serialize function. and unserialized unserialize So
session_start();
$arr=array(0 => cid ,1 => TN201501HP);
$_SESSION['myvar'] =serialize($arr);
$myarr= unserialize($_SESSION['myvar']);
echo $myarr[1];
and if you only want one variable in array try
session_start();
$_SESSION['myvar'] = $array[1];
echo $_SESSION['myvar'];
OTHER TIPS
<?php
//If you want store the value in a session, you need first start the session
session_start();
//I suposse that your array is named $array,
//then if you want get the position 1 use the following line
$_SESSION['var'] = $array[1];
//checking the stored value
echo $_SESSION['var'];
?>
Output:
TN201501HP
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange