This site is mobile accessible. Press the "Tap Here" button to use a different font-size.


Smartphone icons created by Freepik - Flaticon

PHP and JSON

PHP - Looping Through the Values

This example shows how to loop through the values of a PHP associative array:

Peter => 35
Ben => 37
Joe => 43

<?php
// define a $jsonobj
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

// create $arr by running json_decode on $jsonobj with the "true" parameter
$arr = json_decode($jsonobj, true);

// run foreach loop on each $key => $value pair of the $arr and display each result on a new line
foreach($arr as $key => $value) {
  echo $key . " => " . $value . "<br>";
}
?>

I ran the above without the "true" parameter and got the same result.