Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

JSON Decode into SESSION

Is it possibe to decode a 2 dimentional JSON into Session variables?

So for example:-
$json = "{ allowDelete:'true', allowEdit:'true', allowEmail:'true', allowFolder:'false', allowUpload:'true', allowUsers:'true' }";

Open in new window


What Id like is:-
$_SESSION['allowDelete'] = true;
$_SESSION['allowEdit'] = true;
$_SESSION['allowEmail'] = true;
$_SESSION['allowFolder'] = true;
$_SESSION['allowUpload'] = true;
$_SESSION['allowUsers'] = true;

Open in new window


I thought I could just use:-
<?php
    session_start();
    $json = "{ allowDelete:'true', allowEdit:'true', allowEmail:'true', allowFolder:'false', allowUpload:'true', allowUsers:'true' }";

    $_SESSION = JSON_DECODE($json);

    echo "Session Variables are:-";ssss
    print_r($_SESSION);
?>

Open in new window


But it doesnt add the variables into the session array.

Any ideas?
Avatar of DrDamnit
DrDamnit
Flag of United States of America image

Firstly, you need to use json_decode($json, true) to get it into an associative array instead of an object.

But secondly, that is going to return an array.

If you want the array's contents as individual session variables, you'll need to loop through the associative array and create the session variables individually.
Avatar of tonelm54
tonelm54

ASKER

Ok, so Ive managed to loop through and add into the session:-
<?php
    session_start();
    session_destroy();
    $_Session['test'] = "123";

    $json = "{\"allowDelete\":\"true\", \"allowEdit\":\"true\", \"allowEmail\":\"true\", \"allowFolder\":\"false\", \"allowUpload\":\"true\", \"allowUsers\":\"true\"}";
    $jsonDecode = JSON_DECODE($json, true);

    foreach($jsonDecode as $key=>$value)
        {
        $_SESSION[$key] = $value;
        }

    echo "Session Variables are:-";
    print_r($_SESSION);

    echo "Allow-" . $_Session['allowEdit'] . "<br/>";
    echo "Allow-" . $_Session['test'];
?>

Open in new window


What Im confused about, is it outputs:-
stdClass Object
(
    [allowDelete'] => true
    [allowEdit] => true
    [allowEmail] => true
    [allowFolder] => false
    [allowUpload] => true
    [allowUsers] => true
)
Session Variables are:-Array
(
    [allowDelete'] => true
    [allowEdit] => true
    [allowEmail] => true
    [allowFolder] => false
    [allowUpload] => true
    [allowUsers] => true
)
<br />
<b>Notice</b>:  Undefined index: allowEdit in <b>D:\www\Axians\customers\test.php</b> on line <b>25</b><br />
Allow-<br/>Allow-123

What Im confused about, is there the 'test' session variable disapears to in thee print_r($_Session) and why it put [ ] around the array key names ???
Variables names are Case Sensitive in PHP.  You used $_SESSION in your 'foreach' and $_Session on line 25 or so.  The correct one for Session variables is the uppercase version $_SESSION.  The lowercase version $_Session is just another variable and NOT part of the Session.
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial