How can fgetcsv be used to automatically create one-dimensional arrays and then convert them into two-dimensional arrays in PHP?

To automatically create one-dimensional arrays from a CSV file using fgetcsv, you can read each row of the file and store it as a separate array. To convert these one-dimensional arrays into a two-dimensional array, you can push each one-dimensional array into a main array. This way, each row of the CSV file becomes a separate array within the main array.

<?php

// Open the CSV file for reading
$csvFile = fopen('data.csv', 'r');

// Initialize an empty array to store the two-dimensional array
$twoDimensionalArray = [];

// Read each row of the CSV file and store it as a one-dimensional array
while (($data = fgetcsv($csvFile)) !== false) {
    // Push the one-dimensional array into the main array
    $twoDimensionalArray[] = $data;
}

// Close the CSV file
fclose($csvFile);

// Display the two-dimensional array
print_r($twoDimensionalArray);

?>