How can PHP be used to create structured arrays from complex data structures found in a text file, like in the provided example?

To create structured arrays from complex data structures found in a text file using PHP, we can read the file line by line, parse the data, and store it in an array accordingly. We can use functions like explode() or preg_match() to extract specific data elements from each line and then organize them into a structured array.

<?php

// Read the text file line by line
$file = fopen('data.txt', 'r');
$dataArray = [];

while (!feof($file)) {
    $line = fgets($file);

    // Parse the data and structure it into an array
    $parsedData = explode(',', $line);
    
    // Store the parsed data in the main array
    $dataArray[] = [
        'name' => $parsedData[0],
        'age' => $parsedData[1],
        'location' => $parsedData[2]
    ];
}

fclose($file);

// Print the structured array
print_r($dataArray);

?>