PHP选择CSV文件只有第一,中间和最后一列 [英] php select only first, middle and last column from csv file

查看:122
本文介绍了PHP选择CSV文件只有第一,中间和最后一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSV文件,多数民众赞成上传,我需要确定有多少项是CSV文件和第一,中间和最后一列的格式与$头=> $值对的关联数组。我有code,它格式化整个CSV到数组

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;
}

不过,我尽可能确定要做到这一点的方式卡住。任何帮助将是AP preciated。有一件事,可能我想是要使用此功能上面来获取数组,然后写一些其他的功能,只得到这个数组的开始中间和结尾和转储其余的(但仍需要konw如何做到这一点的只要确定开始中间和结尾

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

推荐答案

我会为你​​在文件中读取过程这一点。

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 ] );

您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;
}

这篇关于PHP选择CSV文件只有第一,中间和最后一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆