What are some best practices for extracting specific values from a JSON string in PHP?

When working with JSON strings in PHP, one common task is extracting specific values from the JSON data. To achieve this, you can decode the JSON string into an associative array using the `json_decode` function, and then access the desired values using array notation. It's important to handle cases where the JSON string may be malformed or the key you are trying to access does not exist.

// Sample JSON string
$jsonString = '{"name": "John Doe", "age": 30}';

// Decode the JSON string into an associative array
$data = json_decode($jsonString, true);

// Check if decoding was successful
if ($data !== null) {
    // Access specific values from the array
    $name = $data['name'];
    $age = $data['age'];

    // Output the extracted values
    echo "Name: " . $name . "<br>";
    echo "Age: " . $age;
} else {
    echo "Error decoding JSON string";
}