0

Using PHP, how would I access the inner array values, specific to this example the values specific to each game array, from a json feed similar to this:

Array
(
[startIndex] => 3
[refreshInterval] => 60
[games] => Array
    (
        [0] => Array
            (
                [id] => 2013020004
                [gs] => 5
                [ts] => WEDNESDAY 10/2
                [tsc] => final
                [bs] => FINAL
                [bsc] => final
            )

        [1] => Array
            (
                [id] => 2013020005
                [gs] => 5
                [ts] => WEDNESDAY 10/2
                [tsc] => final
                [bs] => FINAL
                [bsc] =>
            )

I have tried nesting a foreach loop inside a foreach loop similar to this:

foreach ($json as $key => $jsons) {
foreach ($jsons as $my => $value) {
    echo $value; 
}
}
6
  • what does the output of your json call look like? Commented Oct 4, 2013 at 2:30
  • 1
    That doesn't look like JSON Commented Oct 4, 2013 at 2:30
  • 1
    If the data is json, you will want to json_decode it before you access it like an array in php. Commented Oct 4, 2013 at 2:31
  • Can you give us a concrete example of a result you want. Should it be recursive? Commented Oct 4, 2013 at 2:31
  • this is not json, perhaps ir's a javascript array Commented Oct 4, 2013 at 2:34

4 Answers 4

2

If that is an array you are looking at then you can reach the values

foreach($json["games"] as $game) {
   foreach ($game as $key => $value) {
      echo $value; 
   }
}

This should give you the values within each array in the games section.

EDIT: to answer additional question in comment

To get the specific values of the game like the id, the second foreach loop will not be needed. Instead do the following:

foreach($json["games"] as $game) {
   echo $game['id'];
}
Sign up to request clarification or add additional context in comments.

4 Comments

can i use like this? ' $games[id] ' to get the list of id?
@MinSoe -- I added a little more information that you will find helpful. You will get specifically the id of the game. You can do that for each individual key.
What I am trying to do is to get the list of id without looping. Can we do something like this ' $games = $json["games"]; $idlist = $games[id] ' instead of ' foreach($json["games"] as $game) { $idlist[] = $game['id'] } '
No. You will need to loop. To get an independent id, you can use @immulatin's example $json['games'][0]['id']. But to get all of them, you will need to loop.
1

You just access it as an associative array.

$json['games'][0]['id'] // This is 2013020004
$json['games'][1]['id'] // This is 2013020005

You can loop through the games like so:

foreach($json['games'] as $game){
   print_r($game);
}

Comments

0

If you are trying to access this,

    [0] => Array
        (
            [id] => 2013020004
            [gs] => 5
            [ts] => WEDNESDAY 10/2
            [tsc] => final
            [bs] => FINAL
            [bsc] => final
        )

    [1] => Array
        (
            [id] => 2013020005
            [gs] => 5
            [ts] => WEDNESDAY 10/2
            [tsc] => final
            [bs] => FINAL
            [bsc] =>
        )

..then you are doing it right. The only problem is you are trying to echo an array. Trying using print_r($value). If you want a specific value, like the id. You can echo $value['id'];

1 Comment

and, by the way, your data in the question does not look like JSON. Looks more like a regular PHP array
0

I think you could use one of the many example of recursive array_key_search. This way you could simply do :

$firstGame = array_search_key(0, $array);
$firstGameId = $firstGame["id"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.