Skip to content
PHP Logo

How can a PHP developer efficiently store a specific value, like the "id" field, from a JSON output into a PHP variable for further processing?

To efficiently store a specific value, like the "id" field, from a JSON output into a PHP variable, you can decode the JSON string using `json_decode()` and then access the specific field using its key. This allows you to extract the desired value and store it in a PHP variable for further processing.

php
$jsonOutput = '{"id": 123, "name": "John Doe"}';
$data = json_decode($jsonOutput, true);

$id = $data['id'];

// Now $id contains the value 123 from the JSON output