How can JSON be read and output as an array in PHP?
To read and output JSON as an array in PHP, you can use the `json_decode()` function to convert the JSON data into a PHP array. This function takes a JSON string as its parameter and returns a PHP array. You can then iterate over the array and output its values as needed.
<?php
$json_data = '{"name": "John", "age": 30, "city": "New York"}';
$array_data = json_decode($json_data, true);
foreach ($array_data as $key => $value) {
echo $key . ': ' . $value . '<br>';
}
?>
Related Questions
- What are the best practices for handling privilege escalation in PHP scripts that need to interact with system functions?
- What security considerations should be taken into account when sending emails through PHP scripts?
- What steps can be taken to troubleshoot connection issues to mailboxes on a web server using PHP imap_open?