0

I have this php code:

<?php
session_start();
$data = json_decode($_SESSION['data']);
print_r($data);
?>

Which gives my data from the session. I print it and

stdClass Object ( [_readyCallbacks] => Array ( [0] => ) [http] => stdClass Object ( [_cookie] => SESSION_ID=92f5d674-2430-4ee1-bfaf-04d7be2d32a7; M6UserName=8290 ) [magisterSchool] => stdClass Object ( [id] => 2a685bd6-4cab-4d51-bf48-07ac41ad6c7d [name] => CS Vincent van Gogh [url] => https://csvvg.magister.net ) [username] => 8290 [_keepLoggedIn] => 1 [_ready] => 1 [_magisterLoadError] => [_sessionId] => 92f5d674-2430-4ee1-bfaf-04d7be2d32a7 [_id] => 8982 [_personUrl] => https://csvvg.magister.net/api/personen/8982 [_pupilUrl] => https://csvvg.magister.net/api/leerlingen/8982 [_profileInfo] => stdClass Object ( [_firstName] => Robin [_lastName] => Noord [_birthDate] => 1999-01-23T00:00:00.000Z [_id] => 8982 [_officialFirstNames] => Robin [_initials] => R. [_namePrefix] => van der [_officialSurname] => Noord [_birthSurname] => [_birthNamePrefix] => [_useBirthname] => [_isChild] => ) [_messageFolders] => Array ( [0] => stdClass Object ( [_name] => Postvak IN [_unreadMessagesCount] => 0 [_id] => 1 [_parentId] => 0 ) [1] => stdClass Object ( [_name] => Verzonden items [_unreadMessagesCount] => 0 [_id] => 2 [_parentId] => 0 ) [2] => stdClass Object ( [_name] => Verwijderde items [_unreadMessagesCount] => 0 [_id] => 3 [_parentId] => 0 ) [3] => stdClass Object ( [_name] => Mededelingen [_unreadMessagesCount] => 0 [_id] => 4 [_parentId] => 0 ) ) )

So I was wondering, how do I get e.g. the value for firstName (in profileInfo), or e.g. username?

The data I originally get is just JSON:

{
  "_readyCallbacks": [
    null
  ],
  "http": {
    "_cookie": "SESSION_ID=92f5d674-2430-4ee1-bfaf-04d7be2d32a7; M6UserName=8290"
  },
  "magisterSchool": {
    "id": "2a685bd6-4cab-4d51-bf48-07ac41ad6c7d",
    "name": "CS Vincent van Gogh",
    "url": "https:\/\/csvvg.magister.net"
  },
  "username": "8290",
  "_keepLoggedIn": true,
  "_ready": true,
  "_magisterLoadError": null,
  "_sessionId": "92f5d674-2430-4ee1-bfaf-04d7be2d32a7",
  "_id": 8982,
  "_personUrl": "https:\/\/csvvg.magister.net\/api\/personen\/8982",
  "_pupilUrl": "https:\/\/csvvg.magister.net\/api\/leerlingen\/8982",
  "_profileInfo": {
    "_firstName": "Robin",
    "_lastName": "Noord",
    "_birthDate": "1999-01-23T00:00:00.000Z",
    "_id": 8982,
    "_officialFirstNames": "Robin",
    "_initials": "R.",
    "_namePrefix": "van der",
    "_officialSurname": "Noord",
    "_birthSurname": null,
    "_birthNamePrefix": null,
    "_useBirthname": false,
    "_isChild": false
  },
  "_messageFolders": [
    {
      "_name": "Postvak IN",
      "_unreadMessagesCount": 0,
      "_id": 1,
      "_parentId": 0
    },
    {
      "_name": "Verzonden items",
      "_unreadMessagesCount": 0,
      "_id": 2,
      "_parentId": 0
    },
    {
      "_name": "Verwijderde items",
      "_unreadMessagesCount": 0,
      "_id": 3,
      "_parentId": 0
    },
    {
      "_name": "Mededelingen",
      "_unreadMessagesCount": 0,
      "_id": 4,
      "_parentId": 0
    }
  ]
}

The main problem is the fact that it is a nested JSON with {}'s and []'s and _'s, which all make it a bit confusing.

4 Answers4

0

You can access it using

$data['http'] ['username']

In json, an object will be enclosed using {} and translated into an key pair array when using json_decode()

budirec
  • 288
  • 1
  • 5
0

Try to access stdClass object properties in such way:

// assumming that $json_str is your initial json string
$obj = json_decode($json_str);

var_dump($obj->_profileInfo->_firstName);

// the output:
string(5) "Robin"
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

try this : $data->_profileInfo->_firstName;

Mohammad
  • 3,449
  • 6
  • 48
  • 75
0

You can decode as objects and access object properties:

$data = json_decode($_SESSION['data']);
echo $data->_profileInfo->_firstName

or decode as associative array and then:

$data = json_decode($_SESSION['data'], true);
echo $data['_profileInfo']['_firstName']
pH4Lk0n
  • 21
  • 4