How can you create a multidimensional array from data extracted from a .txt file in PHP?
When extracting data from a .txt file in PHP, you can create a multidimensional array by reading the file line by line and splitting each line into an array of values. You can then push these arrays into a parent array to form the multidimensional structure.
$data = array();
$file = fopen("data.txt", "r");
if ($file) {
while (($line = fgets($file)) !== false) {
$values = explode(",", $line);
$data[] = $values;
}
fclose($file);
}
print_r($data);
Related Questions
- How can PHP beginners ensure that their code is efficient and follows best practices when working with CSV files?
- What are the benefits of using fsockopen in PHP for checking website availability compared to other methods?
- How can PHP developers optimize their regex patterns to efficiently extract email addresses from webpages while accounting for variations in formatting and whitespace?