Yeak
Yeak

Reputation: 2538

php select only first, middle and last column from csv file

I have a csv file thats uploaded and I need to determine how many entries are in the csv file and format the first, middle and last columns to a associative array with $header=>$value pairs. I have the code that formats the entire csv to the array

function csv_to_array($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, 1000, $delimiter)) !== FALSE) {
        if(!$header) {
            $header = $row;
        }else{
            $data[] = array_combine($header, $row);
        }
    }

    fclose($handle);
}

  return $data;
}

However I am stuck as far as determining the way to do this. Any help would be appreciated. One thing that may be possible i suppose is to use this function above to get the array and then write some other function to get the beginning middle and end of this array only and dump the rest (but still need to konw how to do it as far as determining the beginning middle and end

Upvotes: 3

Views: 2312

Answers (1)

Patrick Moore
Patrick Moore

Reputation: 13354

I would process this as you're reading in the file.

// how many total columns
$total = count( $row ); 

// get the halfway point (and round up if a decimal)
$middle = ceil( $total/2 );

// Form a new row using the first (0), last ($total-1) and middle ($middle)
$new_row = array( $row[0], $row[ $middle ], $row[ $total-1 ] );

Embedded within your code:

function csv_to_array($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, 1000, $delimiter)) !== FALSE) {

        $total = count( $row ); 
        $middle = ceil( $total/2 );
        $new_row = array( $row[0], $row[ $middle ], $row[ $total-1 ] );

        if(!$header) {
            $header = $new_row;
        }else{
            $data[] = array_combine($header, $new_row);
        }
    }

    fclose($handle);
}

  return $data;
}

You could alleviate some processing power, by assuming each row in the entire file will have the same column count. If so, you need only count once, like so:

function csv_to_array($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, 1000, $delimiter)) !== FALSE) {

        if ( !$total ){ // Verify if we have the total yet, and if not:
            $total = count( $row ); 
            $middle = ceil( $total/2 );
        }
        $new_row = array( $row[0], $row[ $middle ], $row[ $total-1 ] );

        if(!$header) {
            $header = $new_row;
        }else{
            $data[] = array_combine($header, $new_row);
        }
    }

    fclose($handle);
}

  return $data;
}

Upvotes: 6

Related Questions