user2282217
user2282217

Reputation: 91

Fetching JSON data square brackets

I'm wondering how to fetch JSON data from a JSON file that contains square brackets. I have a JSON code like this:

"events": [
    {
        "id": 462467,
        "eventDetails": [
            70,
            1,
            [
                "Swansea City - Manchester City",
                "Swansea City - Manchester City",
                "Swansea-ManCity"
            ],

I'm using foreach statements to get the values for "eventDetails", but how can i sort out the number "1", and also fetch each team name separately? The php statement I am trying to perform is: If the second value is "1", then fetch team names.

This is what I have so far:

 foreach($array2['events'] as $key=>$val)

    {     
         foreach($val['eventDetails'] as $values)
         {

            if ($values['70,1']) { echo "test"; }

         }
    } 

Upvotes: 0

Views: 198

Answers (2)

Ali Sed
Ali Sed

Reputation: 111

In json brackets mean stdClass (object) ,so:

foreach($array2->events as $key=>$val){    
    echo($val->eventDetails[0]);//70
    echo($val->eventDetails[1]);//1
    foreach($val->eventDetails[2] as $team)
         echo $team;
} 

Upvotes: 0

Michal Przybylowicz
Michal Przybylowicz

Reputation: 1668

You can try to use file_get_contents() like this:

$content = file_get_contents( $filename );
$json = json_decode( $content );
print_r( $json );

More information here:

https://php.net/manual/en/function.file-get-contents.php

And here:

https://php.net/manual/en/function.json-decode.php

Upvotes: 1

Related Questions