How can PHP functions like file(), preg_grep(), and json_decode() be utilized in extracting JSON data?
To extract JSON data using PHP functions like file(), preg_grep(), and json_decode(), you can first read the JSON file using file() to get an array of lines. Then, use preg_grep() to filter out the lines containing the JSON data. Finally, use json_decode() to decode the JSON data into a PHP array or object for further processing.
// Read the JSON file into an array of lines
$lines = file('data.json');
// Filter out the lines containing the JSON data
$filtered_lines = preg_grep('/^\s*{.*}\s*$/', $lines);
// Decode the JSON data into a PHP array
$json_data = json_decode(implode('', $filtered_lines), true);
// Access the JSON data as an associative array
echo $json_data['key'];