使用PHP忽略CSV中的列

时间:2019-03-08 15:36:00

标签: php csv fgetcsv fputcsv

我想从csv中仅提取几列,而忽略其余的列 这是所有列的csv

part_number_key,buy_button_rank,category_id,id,brand,vendor_category_id,name,part_number,sale_price,currency,description,url,warranty,general_stock,weight,status,recommended_price,images,characteristics,attachments,vat_id,family,start_date,estimated_stock,reversible_vat_charging,min_sale_price,max_sale_price,offer_details,offer_properties,product_measurements,availability,stock,handling_time,barcode,ean,commission,validation_status,offer_validation_status,ownership,best_offer_sale_price,best_offer_recommended_price,number_of_offers
    DASDSA,1,11,123,brand,,Name,"part number",12.31,RON,description,https://www.url.com,12,10,0,1,12.19,"a:3:{i:0;a:2:{s:3:""url"";s:27:""https://url.com/images.jpg1"";s:12:""display_type"";i:1;}i:1;a:2:{s:3:""url"";s:27:""https://url.com/images.jpg2"";s:12:""display_type"";i:0;}i:2;a:2:{s:3:""url"";s:27:""https://url.com/images.jpg3"";s:12:""display_type"";i:0;}}","a:11:{i:0;a:2:{s:2:""id"";i:8937;s:5:""value"";s:5:""value"";}i:1;a:2:{s:2:""id"";i:8930;s:5:""value"";s:5:""value"";}i:2;a:2:{s:2:""id"";i:8927;s:5:""value"";s:5:""value"";}i:3;a:2:{s:2:""id"";i:8928;s:5:""value"";s:5:""value"";}i:4;a:2:{s:2:""id"";i:5537;s:5:""value"";s:5:""value"";}i:5;a:2:{s:2:""id"";i:8932;s:5:""value"";s:5:""value"";}i:6;a:2:{s:2:""id"";i:8934;s:5:""value"";s:5:""value"";}i:7;a:2:{s:2:""id"";i:8929;s:5:""value"";s:5:""value"";}i:8;a:2:{s:2:""id"";i:7235;s:5:""value"";s:5:""value"";}i:9;a:2:{s:2:""id"";i:6556;s:5:""value"";s:5:""value"";}i:10;a:2:{s:2:""id"";i:5401;s:5:""value"";s:5:""value"";}}",a:0:{},1,"a:3:{s:2:""id"";i:1104;s:4:""name"";s:4:""NAME"";s:14:""family_type_id"";i:244;}",a:0:{},10,,11,200,"a:3:{s:2:""id"";i:2484194;s:13:""warranty_type"";N;s:16:""supply_lead_time"";i:14;}",a:0:{},a:0:{},"a:1:{i:0;a:2:{s:12:""warehouse_id"";i:1;s:2:""id"";i:3;}}","a:1:{i:0;a:2:{s:12:""warehouse_id"";i:1;s:5:""value"";i:10;}}","a:1:{i:0;a:2:{s:12:""warehouse_id"";i:1;s:5:""value"";i:0;}}",a:0:{},a:0:{},"a:5:{s:5:""value"";s:7:""20.0000"";s:4:""type"";s:10:""percentage"";s:2:""id"";s:3:""123"";s:8:""priority"";s:1:""4"";s:7:""created"";s:19:""2010-01-16 11:25:02"";}","a:1:{i:0;a:3:{s:5:""value"";i:9;s:11:""description"";s:22:""Approved documentation"";s:6:""errors"";N;}}","a:3:{s:5:""value"";i:1;s:11:""description"";s:8:""Saleable"";s:6:""errors"";N;}",1,11,11,1
    D3129DKQW,1,456,123,Bluedio,,name,"part number",123,EUR,description,https://url.com,24,1,0,1,12,"a:3:{i:0;a:2:{s:3:""url"";s:27:""https://url.com/images.jpg1"";s:12:""display_type"";i:1;}i:1;a:2:{s:3:""url"";s:27:""https://url.com/images.jpg2"";s:12:""display_type"";i:0;}i:2;a:2:{s:3:""url"";s:27:""https://url.com/images.jpg3"";s:12:""display_type"";i:0;}}","a:14:{i:0;a:2:{s:2:""id"";i:7774;s:5:""value"";s:3:""val"";}i:1;a:2:{s:2:""id"";i:7947;s:5:""value"";s:3:""val"";}i:2;a:2:{s:2:""id"";i:8001;s:5:""value"";s:3:""val"";}i:3;a:2:{s:2:""id"";i:8937;s:5:""value"";s:3:""val"";}i:4;a:2:{s:2:""id"";i:8930;s:5:""value"";s:3:""val"";}i:5;a:2:{s:2:""id"";i:8927;s:5:""value"";s:3:""val"";}i:6;a:2:{s:2:""id"";i:8928;s:5:""value"";s:3:""val"";}i:7;a:2:{s:2:""id"";i:5537;s:5:""value"";s:3:""val"";}i:8;a:2:{s:2:""id"";i:8932;s:5:""value"";s:3:""val"";}i:9;a:2:{s:2:""id"";i:8934;s:5:""value"";s:3:""val"";}i:10;a:2:{s:2:""id"";i:8929;s:5:""value"";s:3:""val"";}i:11;a:2:{s:2:""id"";i:7235;s:5:""value"";s:3:""val"";}i:12;a:2:{s:2:""id"";i:6556;s:5:""value"";s:3:""val"";}i:13;a:2:{s:2:""id"";i:5401;s:5:""value"";s:3:""val"";}}",a:0:{},1,"a:3:{s:2:""id"";i:123456;s:4:""name"";s:4:""Name"";s:14:""family_type_id"";i:244;}",a:0:{},144,,11,200,"a:3:{s:2:""id"";i:242414;s:13:""warranty_type"";N;s:16:""supply_lead_time"";i:14;}",a:0:{},a:0:{},"a:1:{i:0;a:2:{s:12:""warehouse_id"";i:1;s:2:""id"";i:3;}}","a:1:{i:0;a:2:{s:12:""warehouse_id"";i:1;s:5:""value"";i:9;}}","a:1:{i:0;a:2:{s:12:""warehouse_id"";i:1;s:5:""value"";i:0;}}",a:0:{},a:0:{},"a:5:{s:5:""value"";s:7:""20.0000"";s:4:""type"";s:10:""percentage"";s:2:""id"";s:5:""12492"";s:8:""priority"";s:1:""4"";s:7:""created"";s:19:""2010-01-16 11:25:02"";}","a:1:{i:0;a:3:{s:5:""value"";i:9;s:11:""description"";s:22:""Approved documentation"";s:6:""errors"";N;}}","a:3:{s:5:""value"";i:1;s:11:""description"";s:8:""Saleable"";s:6:""errors"";N;}",1,77.31,83.19,1

这是php代码

$handle = fopen("feed.csv", "r");

$columns = array(11, 7, 11, 9, 14, 5, 3, 18);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        echo("<tr>\r\n");
        foreach ($data as $index=>$val) {
                if (in_array($index+1, $columns)) {
                        echo("\t<td>$val</td>\r\n");
                }

        }
        echo("</tr>\r\n");
}

问题出在输出上,输出没有逗号(定界符)或换行符,并且这不是有效的CSV,如何使用fputcsv()重写csv?在这种情况下?

谢谢!

2 个答案:

答案 0 :(得分:0)

这就是我要使用关联键的方式。

$headers = fgetcsv($handle, 1000, ",");
//[part_number_key,buy_button_rank,category_id, ...]

$header_len = count($headers);

$keep = array_flip(['part_number_key']); //<-- keep items in this array

$i=0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
      ++$i;
      if(count($data) != $header_len) die('Delimiter miss match on line '.$i);
     //[DASDSA,1,11, ...]

     $row = array_combine($headers, $data);
     //[part_number_key=>DASDSA,buy_button_rank=>1,category_id=>11, ...]

     $row = array_intersect_key($row, $keep); //or array_diff_key for the opposite
    //[part_number_key=>DASDSA]
}

只要您的列和标题匹配,它就可以工作。

更新

由于此注释,我在上面的代码中添加了列计数检查:

  

警告:array_combine():两个参数应具有相同数量的元素

由您决定如何处理错误,如果列中缺少定界符或标题重复,这通常由您决定。键是唯一的,因此标头名称应该是唯一的。

您可以这样做:

  print_r(array_count_values($header));

答案 1 :(得分:0)

快速简便地设置:

height

将array_map更改为此表格输出。

<?php
$csv = array_map('str_getcsv', file("feed.csv")); // Loads CSV into memory as an array.

// Convert to a key => value Array.
array_walk($csv, function(&$a) use ($csv) {
    $a = array_combine($csv[0], $a);
});

array_shift($csv); // Removes column headers.

// Removes unwanted Columns.
$csv = array_map(function($a){
    unset($a["id"]);
    unset($a["commission"]);
    return $a;
}, $csv);

print_r($csv);