When passing URL parameters using JSON encoding, it becomes necessary to extract and process the encoded values for further data manipulation. This can be effectively accomplished using PHP's json_decode() function.
Consider the following code snippet:
<code class="php">$json = json_encode([ 'countryId' => $_GET['CountryId'], 'productId' => $_GET['ProductId'], 'status' => $_GET['ProductId'], 'opId' => $_GET['OpId'] ]); echo $json;</code>
This code generates a JSON response similar to:
{ "countryId":"84", "productId":"1", "status":"0", "opId":"134" }
To parse this JSON response and extract the individual values, we can utilize json_decode():
<code class="php">$json = '{"countryId":"84","productId":"1","status":"0","opId":"134"}'; $json = json_decode($json, true); echo $json['countryId']; // Output: 84 echo $json['productId']; // Output: 1 echo $json['status']; // Output: 0 echo $json['opId']; // Output: 134</code>
By setting the second argument of json_decode() to true, the function returns an associative array instead of an object. This allows for convenient access to the decoded values using array keys.
The above is the detailed content of How to Extract Values from JSON-Encoded Data in PHP?. For more information, please follow other related articles on the PHP Chinese website!