Necrospasm
Necrospasm

Reputation: 11

Find if csv has a specific header and if yes, format that specific value

I've been struggling with this PHP problem for a while now. I have a csv file with client info which looks like this.

clientName, clientNumber, clientStatus
Name1,123456,1
Name2,123457,0

now, the problem is the following. sometimes the csv has a date of birth too...as follows :

clientName, clientNumber, clientDOB, clientStatus
Name1,123456,01/10/1980,1
Name2,123457,1980-10-01,0

as you can see the dates are in different format. Whilst converting the csv into an array, i need to check if the in the csv there is clientDOB, and if yes, format it to mysql.

function dateToMysql($date) {
    $mysqlDate = date("Y-m-d",strtotime(str_replace('/','-',$date)));

    return $mysqlDate;
}

function csvToArray($filename='', $delimiter=',') {
    if(!file_exists($filename) || !is_readable($filename)) {
        return FALSE;
    }

    $header = NULL;
    $data = array();

    if (($handle = fopen($filename, 'r')) !== FALSE) {
        while (($row = fgetcsv($handle, 10000, $delimiter)) !== FALSE) {
            if(!$header) {
                $header = $row;
            } else {

                if (in_array("clientDOB", $header)) {
                    //echo dateToMysql($header['clientDOB'][$row])."<br>";
                    $data[] = dateToMysql(array_combine($header, $row));
                } else {
                    $data[] = array_combine($header, $row);
                }
            }
        }
        fclose($handle);
    }
    return $data;
}

echo "<pre>";
print_r(csvToArray($_FILES['csvFile']['name']));
echo "</pre>";

Any help would be appreciated. Thank you

Upvotes: 1

Views: 622

Answers (1)

Kenney
Kenney

Reputation: 9093

Here's an updated function:

function csvToArray($filename='', $delimiter=',') {
    if(!file_exists($filename) || !is_readable($filename)) {
        return FALSE;
    }

    $header = NULL;
    $data = array();

    if (($handle = fopen($filename, 'r')) !== FALSE) {
        while (($row = fgetcsv($handle, 10000, $delimiter)) !== FALSE) {
            $row = array_map('trim', $row);

            if (!$header)
                $header = $row;
            else
            {
              $row = array_combine($header, $row);
              if ( isset( $row[ 'clientDOB' ] ) )
                  $row[ 'clientDOB' ] = date('Y-m-d', strtotime( $row[ 'clientDOB' ] ) );
              $data[] = $row;
            }
        }
        fclose($handle);
    }
    return $data;
}

Notable changes:

  • $row = array_map('trim', $row) makes sure there's no surrounding spaces around the names and values, otherwise the ['clientDOB'] won't match (because it's [' clientDOB'].

  • changing the value of $row['clientDOB'] before appending the $row to $data. Your dateToMysql function expects a $date but instead it was passed an associative array.

  • using strtotime no replacing is needed: it can handle both date formats.

Upvotes: 1

Related Questions