Home >Backend Development >PHP Tutorial >How to Retrieve Values from JSON-Encoded Data in PHP as Individual Variables?
Retrieving Values from JSON-Encoded Data in PHP
You can parse the JSON-encoded data using json_decode(). However, to access the values as individual variables, you need to decode the JSON into an array.
Example:
Consider the following JSON-encoded data:
{"countryId":"84","productId":"1","status":"0","opId":"134"}
To decode the data into an array, use:
<code class="php">$json = json_decode($json_string, true);</code>
The above code will convert the JSON string into an associative array. You can then access the values using their respective keys:
<code class="php">$countryId = $json['countryId']; $productId = $json['productId']; $status = $json['status']; $opId = $json['opId'];</code>
Output:
Each variable will now contain the corresponding value from the JSON-encoded data:
$countryId = 84 $productId = 1 $status = 0 $opId = 134
The above is the detailed content of How to Retrieve Values from JSON-Encoded Data in PHP as Individual Variables?. For more information, please follow other related articles on the PHP Chinese website!