Via Cà Matta 2 - Peschiera Borromeo (MI)
+39 02 00704272
info@synaptica.info

PHP json_decode() Function

Digital Innovation Partner

PHP json_decode() Function

Decode JSON object with an array value:

$jsonobj = '{"dataset":[{"field1":"hello","field2":0,"field3":1},{"field1":"hola","field2":2,"field3":3}]}'; 

$obj = json_decode($jsonobj); // it returns object
 
$array_dataset = $obj->dataset; 

for ($i = 0; $i < count($array_dataset); $i++) { 
	$singoloArray = $array_dataset[$i]; 
	foreach($singoloArray as $field => $value) { 
		echo $field . " => " . $value . "<br>"; 
	} 
}

Decode array of JSON object:

$jsonobj = '[{"field1":"hello","field2":0,"field3":1},{"field1":"hola","field2":2,"field3":3}]';

$obj = json_decode($jsonobj, true); // it returns array

for ($i = 0; $i < count($obj); $i++) { 
	$singoloArray = $obj[$i];
	foreach($singoloArray as $field => $value) { 
		echo $field . " => " . $value . "<br>"; 
	} 
}

Try this on https://3v4l.org/