What are the potential challenges when converting preformatted text into a 2D array in PHP?

When converting preformatted text into a 2D array in PHP, one potential challenge is parsing the text correctly to separate the rows and columns. This can be solved by using PHP functions like explode() to split the text by newline characters and then by a delimiter to separate columns. Another challenge could be handling empty rows or columns, which may require additional validation and error checking.

// Example code snippet to convert preformatted text into a 2D array
$preformattedText = "1,2,3\n4,5,6\n7,8,9";

$rows = explode("\n", $preformattedText);
$matrix = [];

foreach ($rows as $row) {
    $matrix[] = explode(",", $row);
}

// Output the 2D array
print_r($matrix);