Question

I'm building an application which's main data comes from an uploaded spreadsheet file. Right now it reads data from columns I, J, K and L, but I would like to let the user specify the columns in which the data is and read data from those.

This is my current code to read the columns:

    for($column = 'I'; $column != 'M'; $column++) 
    {
    if($worksheet->cellExists($column.$row))
    {
        $cell = $worksheet->getCell($column.$row)->getValue();
        switch($column){
        case 'I':
            // Do something
            break;
        case 'J':
            // Do something
            break;
        case 'K':
            // Do something
            break;
        case 'L':
            // Do something
            break;
        }
    }
    }

The columns needed to be read would be in an array, eg.: [0]=>A, [1]=>K, [2]=>Z, [3]=>C

Was it helpful?

Solution

A foreach would do the trick. It allows iterating on an array.

Just replace your for loop by a foreach:

$columns = array('A', 'K', 'Z', 'C');
foreach ($columns as $column)
{
    // ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top