PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
error_reporting(E_ALL);
session_start();
$_SESSION['foo']['bar']['foobar'] = 'w00t';
preg_match('#{SESSION\.([a-z0-9\.]+)}#i', '{SESSION.foo.bar.foobar}', $matches);
if (count($matches[1])) {
$keys = explode('.', $matches[1]);
// if only one key lets convert it to an array anyways
if (count($keys) == 0) {
$keys = array($matches[1][0]);
}
// holds the last key that is recursively searched
$lastkey = '';
foreach ($keys as $key) {
// determine if the key actually exists in the session
if (empty($lastkey) && isset($_SESSION[$key])) {
$lastkey = $_SESSION[$key];
}
// determine if the last instance is an array and if we need to continue
elseif (!empty($lastkey) && is_array($lastkey) && isset($lastkey[$key])) {
$lastkey = $lastkey[$key];
}
// the value does not exist in the session
else {
//reset the value to blank
$lastkey = '';
break;
}
}
echo $lastkey;
}