What are some best practices for debugging PHP code that involves arrays, especially when dealing with CSV files?

When debugging PHP code that involves arrays, especially when dealing with CSV files, it is important to first ensure that the CSV file is being properly read and parsed into an array. One common issue is incorrectly accessing array elements, so it is helpful to use var_dump() or print_r() to inspect the array structure. Additionally, checking for errors in reading the CSV file or parsing its contents can help identify issues.

// Read CSV file into an array
$csvFile = 'data.csv';
$csvData = [];
if (($handle = fopen($csvFile, 'r')) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
        $csvData[] = $data;
    }
    fclose($handle);
}

// Debugging: Print array structure
var_dump($csvData);

// Accessing array elements
foreach ($csvData as $row) {
    echo $row[0] . ', ' . $row[1] . '<br>';
}