简体   繁体   English

PHP,CSV列分成单独的数组

[英]PHP, CSV columns into separate arrays

Basically, i want to make a system where a user can upload a CSV file and pick what columns they want to upload/process 基本上,我想建立一个用户可以上传CSV文件并选择他们要上传/处理哪些列的系统

I can do this fine by using fopen and foreach, but since the number of the column may vary from CSV to CSV.... 我可以通过使用fopen和foreach来做到这一点,但是由于列数可能因CSV或CSV有所不同。

i can store the picked columns in an array, eg picked[]= "1,2,4"; 我可以将选择的列存储在数组中,例如picked [] =“ 1,2,4”; //from 1,2,3,4 columns comma separated or anyway i want. //从1,2,3,4列中以逗号分隔或无论如何我都想要

but how can i use something like list(1,2,4) = explode("," , theData[]); 但是我该如何使用list(1,2,4)= explode(“,”,theData [])之类的东西;

where i can load 1,2,4, in there dynamically, or even 1,2,3,4 and then i can ignore 3. 我可以在其中动态加载1,2,4,甚至1,2,3,4,然后我可以忽略3。

well the csv is sort of a 2 dimensional array as you got the row index and column index. 当您获得行索引和列索引时,csv就是二维数组。

First you will have to split your line breaks (\\n) to split everything in seperate rows. 首先,您必须将换行符(\\ n)拆分为单独的行。 Then each row has to be seperated by comma. 然后,每行必须用逗号分隔。 For the first row you will have the column names. 对于第一行,您将具有列名。

All this can easily be coded yourself... 所有这些都可以轻松地自己编写。

Or you could use the fgetcsv function 或者您可以使用fgetcsv函数

Function gets explained on: http://php.net/manual/en/function.fgetcsv.php 有关功能的说明,请参见http : //php.net/manual/en/function.fgetcsv.php

You can unset the columns you don't need from each row: 您可以从每一行中取消设置不需要的列:

$theData = array(
    array( 'col1', 'col2', 'col3', 'col4', 'col5' ),
    array( 'col1', 'col2', 'col3', 'col4', 'col5' ),
    array( 'col1', 'col2', 'col3', 'col4', 'col5' )
    );
$picked = '1, 3, 5';

$totalColumns = count( $theData[0] );
$columns = explode( ',', $picked );
foreach( $theData as $k => &$thisData ) {
    for( $x = 1; $x < $totalColumns + 1; $x++ ) {
        if( ! in_array( $x, $columns ) ) {
            unset( $thisData[$x - 1] );
        }
    }
}

Watch out for the 0-index adjustments in the for loop and the unset - this is because you have used column numbers that are non 0-indexed in your example. 请注意for循环中的0索引调整和未unset -这是因为在示例中您使用了非0索引的列号。

You could use array_intersect_key() : "returns an array containing all the entries of array1 which have keys that are present in all the arguments." 您可以使用array_intersect_key() :“返回一个数组,其中包含array1的所有条目,这些条目的所有参数中都包含键。”

http://www.php.net/manual/en/function.array-intersect-key.php http://www.php.net/manual/zh/function.array-intersect-key.php

So first you need to process the data line by line, eg to an array that looks like 因此,首先您需要逐行处理数据,例如处理一个看起来像

$data[column] = array(entries):

Example: 例:

$data = array(
  1 => array (
    entry1,
    entry2,
    etc
  ),
  2 => etc
);

Then you show the user which columns there are, the user selects the columns he likes to use, eg: 然后,向用户显示存在哪些列,用户选择他喜欢使用的列,例如:

$selected = array(1,2,4);

Then do an intersect to get an array with the selected columns: 然后进行相交以获取具有所选列的数组:

 $use = array_intersect_key($data, $selected);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM