PHP json_decode() Function
Decode JSON object with an array value:
1 2 3 4 5 6 7 8 9 10 11 12 |
$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:
1 2 3 4 5 6 7 8 9 10 |
$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/