0

I am not sure how I would get access to data in a ARRAY like the one I have showen below.

As im trying to get summonerId, but it wont get it.

My current code is:

$getTierlist = all the data showen

$summonerId = $getTierlist['summonerId'];

I have also tried

$summonerId = $getTierlist->summonerId;

and

$summonerId = $getTierlist['29161162']->summonerId;

And nothing will work. How can I get the data I want out of this?

{
"29161162": {
"summonerId": 29161162,
"pages": [
  {
    "id": 24193964,
    "name": "nida",
    "current": false,
    "slots": [
      {
        "runeSlotId": 1,
        "runeId": 5273
      },
      {
        "runeSlotId": 2,
        "runeId": 5273
      },
      {
        "runeSlotId": 3,
        "runeId": 5273
      },
      {
        "runeSlotId": 4,
        "runeId": 5273
      },
      {
        "runeSlotId": 5,
        "runeId": 5273
      },
      {
        "runeSlotId": 6,
        "runeId": 5273
      },
      {
        "runeSlotId": 7,
        "runeId": 5273
      },
      {
        "runeSlotId": 8,
        "runeId": 5273
      },
      {
        "runeSlotId": 9,
        "runeId": 5273
      },
      {
        "runeSlotId": 10,
        "runeId": 5317
      },
      {
        "runeSlotId": 11,
        "runeId": 5317
      },
      {
        "runeSlotId": 12,
        "runeId": 5317
      },
      {
        "runeSlotId": 13,
        "runeId": 5317
      },
      {
        "runeSlotId": 14,
        "runeId": 5317
      },
      {
        "runeSlotId": 15,
        "runeId": 5317
      },
      {
        "runeSlotId": 16,
        "runeId": 5317
      },
      {
        "runeSlotId": 17,
        "runeId": 5317
      },
      {
        "runeSlotId": 18,
        "runeId": 5317
      },
      {
        "runeSlotId": 19,
        "runeId": 5297
      },
      {
        "runeSlotId": 20,
        "runeId": 5297
      },
      {
        "runeSlotId": 21,
        "runeId": 5297
      },
      {
        "runeSlotId": 22,
        "runeId": 5289
      },
      {
        "runeSlotId": 23,
        "runeId": 5289
      },
      {
        "runeSlotId": 24,
        "runeId": 5289
      },
      {
        "runeSlotId": 25,
        "runeId": 5289
      },
      {
        "runeSlotId": 26,
        "runeId": 5289
      },
      {
        "runeSlotId": 27,
        "runeId": 5289
      },
      {
        "runeSlotId": 28,
        "runeId": 5357
      },
      {
        "runeSlotId": 29,
        "runeId": 5357
      },
      {
        "runeSlotId": 30,
        "runeId": 5357
      }
    ]
  },
  {
    "id": 24193965,
    "name": "adc",
    "current": false,
    "slots": [
      {
        "runeSlotId": 1,
        "runeId": 5245
      },
      {
        "runeSlotId": 2,
        "runeId": 5245
      },
      {
        "runeSlotId": 3,
        "runeId": 5245
      },
      {
        "runeSlotId": 4,
        "runeId": 5245
      },
      {
        "runeSlotId": 5,
        "runeId": 5245
      },
      {
        "runeSlotId": 6,
        "runeId": 5245
      },
      {
        "runeSlotId": 7,
        "runeId": 5245
      },
      {
        "runeSlotId": 8,
        "runeId": 5245
      },
      {
        "runeSlotId": 9,
        "runeId": 5245
      },
      {
        "runeSlotId": 10,
        "runeId": 5317
      },
      {
        "runeSlotId": 11,
        "runeId": 5317
      },
      {
        "runeSlotId": 12,
        "runeId": 5317
      },
      {
        "runeSlotId": 13,
        "runeId": 5317
      },
      {
        "runeSlotId": 14,
        "runeId": 5317
      },
      {
        "runeSlotId": 15,
        "runeId": 5317
      },
      {
        "runeSlotId": 16,
        "runeId": 5317
      },
      {
        "runeSlotId": 17,
        "runeId": 5317
      },
      {
        "runeSlotId": 18,
        "runeId": 5317
      },
      {
        "runeSlotId": 19,
        "runeId": 5289
      },
      {
        "runeSlotId": 20,
        "runeId": 5289
      },
      {
        "runeSlotId": 21,
        "runeId": 5289
      },
      {
        "runeSlotId": 22,
        "runeId": 5289
      },
      {
        "runeSlotId": 23,
        "runeId": 5289
      },
      {
        "runeSlotId": 24,
        "runeId": 5289
      },
      {
        "runeSlotId": 25,
        "runeId": 5289
      },
      {
        "runeSlotId": 26,
        "runeId": 5289
      },
      {
        "runeSlotId": 27,
        "runeId": 5289
      },
      {
        "runeSlotId": 28,
        "runeId": 5337
      },
      {
        "runeSlotId": 29,
        "runeId": 5337
      },
      {
        "runeSlotId": 30,
        "runeId": 5337
      }
    ]
  },
  {
    "id": 60148113,
    "name": "zed",
    "current": false,
    "slots": [
      {
        "runeSlotId": 1,
        "runeId": 5253
      },
      {
        "runeSlotId": 2,
        "runeId": 5253
      },
      {
        "runeSlotId": 3,
        "runeId": 5253
      },
      {
        "runeSlotId": 4,
        "runeId": 5253
      },
      {
        "runeSlotId": 5,
        "runeId": 5253
      },
      {
        "runeSlotId": 6,
        "runeId": 5253
      },
      {
        "runeSlotId": 7,
        "runeId": 5253
      },
      {
        "runeSlotId": 8,
        "runeId": 5245
      },
      {
        "runeSlotId": 9,
        "runeId": 5245
      },
      {
        "runeSlotId": 10,
        "runeId": 5317
      },
      {
        "runeSlotId": 11,
        "runeId": 5317
      },
      {
        "runeSlotId": 12,
        "runeId": 5317
      },
      {
        "runeSlotId": 13,
        "runeId": 5317
      },
      {
        "runeSlotId": 14,
        "runeId": 5317
      },
      {
        "runeSlotId": 15,
        "runeId": 5317
      },
      {
        "runeSlotId": 16,
        "runeId": 5317
      },
      {
        "runeSlotId": 17,
        "runeId": 5317
      },
      {
        "runeSlotId": 18,
        "runeId": 5317
      },
      {
        "runeSlotId": 19,
        "runeId": 5289
      },
      {
        "runeSlotId": 20,
        "runeId": 5289
      },
      {
        "runeSlotId": 21,
        "runeId": 5289
      },
      {
        "runeSlotId": 22,
        "runeId": 5289
      },
      {
        "runeSlotId": 23,
        "runeId": 5289
      },
      {
        "runeSlotId": 24,
        "runeId": 5289
      },
      {
        "runeSlotId": 25,
        "runeId": 5289
      },
      {
        "runeSlotId": 26,
        "runeId": 5289
      },
      {
        "runeSlotId": 27,
        "runeId": 5289
      },
      {
        "runeSlotId": 28,
        "runeId": 5345
      },
      {
        "runeSlotId": 29,
        "runeId": 5345
      },
      {
        "runeSlotId": 30,
        "runeId": 5335
      }
    ]
  },
  {
    "id": 60148114,
    "name": "one",
    "current": true,
    "slots": [
      {
        "runeSlotId": 1,
        "runeId": 5253
      },
      {
        "runeSlotId": 2,
        "runeId": 5253
      },
      {
        "runeSlotId": 3,
        "runeId": 5253
      },
      {
        "runeSlotId": 4,
        "runeId": 5253
      },
      {
        "runeSlotId": 5,
        "runeId": 5253
      },
      {
        "runeSlotId": 6,
        "runeId": 5253
      },
      {
        "runeSlotId": 7,
        "runeId": 5253
      },
      {
        "runeSlotId": 8,
        "runeId": 5253
      },
      {
        "runeSlotId": 9,
        "runeId": 5253
      },
      {
        "runeSlotId": 10,
        "runeId": 5317
      },
      {
        "runeSlotId": 11,
        "runeId": 5317
      },
      {
        "runeSlotId": 12,
        "runeId": 5317
      },
      {
        "runeSlotId": 13,
        "runeId": 5317
      },
      {
        "runeSlotId": 14,
        "runeId": 5317
      },
      {
        "runeSlotId": 15,
        "runeId": 5317
      },
      {
        "runeSlotId": 16,
        "runeId": 5317
      },
      {
        "runeSlotId": 17,
        "runeId": 5317
      },
      {
        "runeSlotId": 18,
        "runeId": 5317
      },
      {
        "runeSlotId": 19,
        "runeId": 5289
      },
      {
        "runeSlotId": 20,
        "runeId": 5289
      },
      {
        "runeSlotId": 21,
        "runeId": 5289
      },
      {
        "runeSlotId": 22,
        "runeId": 5289
      },
      {
        "runeSlotId": 23,
        "runeId": 5289
      },
      {
        "runeSlotId": 24,
        "runeId": 5289
      },
      {
        "runeSlotId": 25,
        "runeId": 5289
      },
      {
        "runeSlotId": 26,
        "runeId": 5289
      },
      {
        "runeSlotId": 27,
        "runeId": 5289
      },
      {
        "runeSlotId": 28,
        "runeId": 5335
      },
      {
        "runeSlotId": 29,
        "runeId": 5335
      },
      {
        "runeSlotId": 30,
        "runeId": 5335
      }
    ]
  }
]
}
}
  • How is this different from your last question http://stackoverflow.com/questions/40441456/accessing-data-from-json-in-php – Barmar Nov 05 '16 at 19:40

2 Answers2

0

Try converting that JSON output to an array first:

$theList = json_decode($getTierlist, TRUE);
var_dump($theList['29161162']['summonerId']));
Zach
  • 517
  • 2
  • 8
  • I already have it converted to an array this is just the part of the code to get what i need from the array – ForsakenHound Nov 05 '16 at 19:34
  • Okay, then it looks like a multi-dimensional array. Maybe something like: – Zach Nov 05 '16 at 19:35
  • foreach($theList as $key => $subkey) { $summonerId = key($key); foreach($theList[$summonerId]['pages'] as $page => $data) { // do something with $page and $data } } – Zach Nov 05 '16 at 19:39
0

Your variable $getTierlist seems to be a JSON string and not a PHP array. You need to convert it to a PHP array before you can access it's elements like an array:

$getTierlist_Array = json_decode($getTierlist, true);

Note the second argument, true, which implies that the function should return an object instead of an array. After doing the above, you can access your summonerId like this:

$summmonerId = $getTierList_Array['29161162']['summonerId'];
nvkrj
  • 1,002
  • 1
  • 7
  • 17