How can the explode function be used to separate data in a text file into an array in PHP?
To separate data in a text file into an array in PHP, you can use the explode function to split the text based on a specific delimiter, such as a comma or a new line. This will create an array where each element corresponds to a separate piece of data from the text file. You can then loop through the array to access and process each individual data point as needed.
// Read the contents of the text file into a variable
$file_contents = file_get_contents('data.txt');
// Split the text into an array based on a specific delimiter, such as a comma
$data_array = explode(',', $file_contents);
// Loop through the array to access and process each individual data point
foreach ($data_array as $data) {
echo $data . "<br>";
}