Working with JSON in PHP: Accessing Array Items

This afternoon I spent a little time playing around with JSON & PHP using repl.it so that I can figure out how to build the next part of my WingsuitGP app. For this app I’m sending a fairly large amount of JSON data from an iPhone app back to a server running PHP, and need to parse all of the JSON data so that I can stick it in a MySQL database for further analysis. Not sure if this will be of any use to anyone other than myself, but here is what I have so far:

$rawJSON = '
{
    "location": [
        {
            "latitude": 30.00000001,
            "longitude": 30.0000005,
            "timestamp": 2342342342
        },
        {
            "latitude": 30.00000029,
            "longitude": 30.00000023,
            "timestamp": 2342342343
        }
    ],
    "altitude": [
        {
            "pressure": 90.00000001,
            "timestamp": 2342342345
        },
        {
            "pressure": 91.00000029,
            "timestamp": 2342342347
        }
    ]
}
';
$decodedJSON = json_decode($rawJSON);
$latitude[0] = $decodedJSON->{'location'}[0]->{'latitude'};
$longitude[0] = $decodedJSON->{'location'}[0]->{'longitude'};
$locationTimestamp[0] = $decodedJSON->{'location'}[0]->{'timestamp'};
$latitude[1] = $decodedJSON->{'location'}[1]->{'latitude'};
$longitude[1] = $decodedJSON->{'location'}[1]->{'longitude'};
$locationTimestamp[1] = $decodedJSON->{'location'}[1]->{'timestamp'};
$altitude[0] = $decodedJSON->{'altitude'}[0]->{'pressure'};
$altitudeTimestamp[0] = $decodedJSON->{'altitude'}[0]->{'timestamp'};
$altitude[1] = $decodedJSON->{'altitude'}[1]->{'pressure'};
$altitudeTimestamp[1] = $decodedJSON->{'altitude'}[1]->{'timestamp'};
echo "\n";
echo "Latitude Point 0: {$latitude[0]}" . "\n";
echo "Longitude Point 0: {$longitude[0]}" . "\n";
echo "Location Timestamp 0: {$locationTimestamp[0]}" . "\n";
echo "\n";
echo "Latitude Point 1: {$latitude[1]}" . "\n";
echo "Longitude Point 1: {$longitude[1]}" . "\n";
echo "Location Timestamp 1: {$locationTimestamp[1]}" . "\n";
echo "\n";
echo "Altitude Reading 0: {$altitude[0]}" . "\n";
echo "Altitude Timestamp 0: {$altitudeTimestamp[0]}" . "\n";
echo "\n";
echo "Altitude Reading 1: {$altitude[1]}" . "\n";
echo "Altitude Timestamp 1: {$altitudeTimestamp[1]}" . "\n";
echo "\n";

 

topherPedersen