Browse json array with php

1

I need to go through this array and extract the data heartRate and deviceNumber in separate variables.

{
"bodys": {
"bikeArrays": [],
"heartRateArrays": [
{
"heartRate": "88",
"deviceNumber": "302831"
}
]
},
"status": 1
}

El codigo que estoy utilizando es el siguiente:



$url = 'http://localhost:8090/rc900/openapi/datas?deviceNumbers=302831'; 
//me devuelve el array de arriba

$json = file_get_contents($url);
$obj = json_decode($json);


$datos = $obj->bodys->heartRateArrays;
echo json_encode($datos);


?>

obtengo como resultado cuando hago echo json_encode($datos);

[{"heartRate":"58","deviceNumber":"302831"}]
  

I would like to know how to go over the result above to put it in   two separate variables

    
asked by Juan Ignacio Bracamonte 27.06.2018 в 20:12
source

4 answers

0

You can do it like this:

$hearRate = $obj->bodys->heartRateArrays[0]->heartRate;
$deviceNo = $obj->bodys->heartRateArrays[0]->deviceNumber;
    
answered by 27.06.2018 / 20:17
source
0

Modifying the response of

a bit
  

alanfcm

test as follows:

$hearRate = $obj->bodys->hearRateArrays[0][heartRate];
$deviceNo = $obj->bodys->hearRateArrays[0][deviceNumber];
    
answered by 27.06.2018 в 20:38
0

$ obj = json_decode ($ json, true);

Convert it to array with the second parameter $ assoc = true and work it as usual with an associative array and foreach.

    
answered by 27.06.2018 в 21:01
0

Adding examples to the response of colleague Mario.

Decode the JSON

$ obj = json_decode ($ json, true);

Go through the Fix

foreach ($resultado_json as $key => $value) {
    foreach ($value as $key2 => $value2) {
        echo $heartRate= $value2[0]['heartRate'];
        echo $deviceNumber= $value2[0]['deviceNumber'];
    };            
};

print_r($heartRate); //88
print_r($deviceNumber); //302831

Store your data in Variables

$ heartRate

$ deviceNumber

    
answered by 13.10.2018 в 10:07