al leer desde MySQL un Json me devuelve lo siguiente:
Array
(
[0] => stdClass Object
(
[json] => {"port": 1, "app_id": "ltech_app1", "dev_id": "nodo_ltech", "counter": 120, "metadata": {"time": "2020-02-27T00:43:57.319462198Z", "airtime": 66816000, "gateways": [{"snr": 9, "rssi": -43, "time": "", "gtw_id": "eui-246f28ffff252590", "channel": 0, "altitude": 14, "latitude": 52.23736, "rf_chain": 0, "longitude": 5.97865, "timestamp": 832217207}], "data_rate": "SF7BW125", "frequency": 868.1, "modulation": "LORA", "coding_rate": "4/5"}, "payload_raw": "TmVzdG9yIHBydWViYSE=", "payload_fields": {"message": "Nestor prueba!"}, "hardware_serial": "00152C2E8F355A97"}
)
[1] => stdClass Object
(
[json] => "Nestor prueba!"
)
) pero cuando lo quiero explorar para sacar los datos que necesito me dice: json_decode() expects parameter 1 to be string, array given
este es el codigo
<?php
// Define recursive function to extract nested values
function printValues($arr) {
global $count;
global $values;
// Check input is an array
if(!is_array($arr)){
die("ERROR: Input is not an array");
}
/*
Loop through array, if value is itself an array recursively call the
function else add the value found to the output items array,
and increment counter by 1 for each value found
*/
foreach($arr as $key=>$value){
if(is_array($value)){
printValues($value);
} else{
$values[] = $value;
$count++;
}
}
// Return total count and values found in array
return array('total' => $count, 'values' => $values);
}
// Decode JSON data into PHP associative array format
$arr = ( json_decode($data, true) );
// Call the function and print all the values
$result = printValues($arr);
{?>
ahora si le fuerzo el mensaje reemplazando data por la variable json y funciona perfectamente, que me esta faltando.
$json = '{"port": 1,"app_id": "ltech_app1","dev_id": "nodo_ltech","counter": 120,"metadata": {"time": "2020-02-27T00:43:57.319462198Z","airtime": 66816000,"gateways":[{"snr": 9, "rssi": -43, "time": "", "gtw_id": "eui-246f28ffff252590", "channel": 0, "altitude": 14, "latitude": 52.23736, "rf_chain": 0, "longitude": 5.97865, "timestamp": 832217207}],';
de esta manera obtengo los datos, conectándome a la base de MySQL lo que me devuelve el mensaje publicado al inicio
// Crear conexión
$cn = mysqli_connect("localhost","admin_ltech","xxxxxxx","admin_ltech");
// Verifica la conexión
if ( mysqli_connect_errno() ) {
echo "Error: Ups! Hubo problemas con la conexión. Favor de intentar nuevamente.";
} else {
$strsql = "SELECT * FROM `lora`";
$rs = mysqli_query($cn, $strsql);
$total_rows = $rs->num_rows;
if ($total_rows > 0 ) {
while ($row = $rs->fetch_object()){
$data[] = $row;
}
//print_r($data);
//echo( json_encode($data) );
}
}
?> ```