【问题标题】:How to convert few columns as JSON array in php while csv importcsv导入时如何在php中将几列转换为JSON数组
【发布时间】:2018-05-08 10:51:53
【问题描述】:

我有一个 csv 文件,请看下面这张我的 CSV 文件

这是我的桌子

我需要帮助来拆分几列并将其转换为数组并插入到表中,假设我想从数组中排除第一列“product_id”并将所有列保留为数组并插入到表中,因此预期的查询将类似于insert into process_detail (product_id,product_attributes) values ($product_id,'[{"color":"blue","style":"classic","material_type":"etc","length":"35 cm","price":"11.25","product_description":"etc"}]'; 注意:我在 CSV 文件第一行中的 csv 标题将与 Mysql 表标题完全匹配

这是我的 php

 function convert($string)
{
    return htmlspecialchars($string,ENT_QUOTES);
}

$columnArray    = array();
$dataArray      = array();
$firstRule      = true;

while ($data = fgetcsv ($source, 1000, ","))
{
    if($firstRule)
    {
        foreach($data as $columnName)
        {
            $columnArray[] = $columnName;
        }

        $firstRule = false;
    }
    else
    {
        $rule = array();
        for($i = 0; $i < count($data) ; $i++)
        {
            $rule[$columnArray[$i]] = $data[$i];
        }
        $dataArray[] = $rule;
    }
}

     foreach($dataArray as $data)
        {
            // here i am stuck, i would want to json_encode of few columns and insert into table
        }

回显 $product_id; var_dump($bound_values);

mlarray(2) { ["product_id"]=&gt; string(2) "ml" ["product_attributes"]=&gt; string(1588) "{"cv_id":"weight_2_kg","sv_id":"label_3","collection_id":"length_3_cm","season":"width_3_cm","hit":"height_3_cm","tier":"diameter_3_cm","style_no":"size_3_ml","base_product_id":"weight_3_kg","product_title":"label_4","introduction":"length_4_cm","color_name":"width_4_cm","price_a":"height_4_cm","color_price_a":"diameter_4_cm","sku_price_a":"size_4_ml","price_b":"weight_4_kg","color_price_b":"label_5","price_c":"length_5_cm","color_price_c":"width_5_cm","sku_price_c":"height_5_cm","product_availability_b":"diameter_5_cm","product_availability_c":"size_5_ml","product_availability_d":"weight_5_kg","returnable_a":"label_6","returnable_b":"length_6_cm","returnable_c":"width_6_cm","collect_a":"height_6_cm","collect_b":"diameter_6_cm","collect_c":"size_6_ml","brand":"weight_6_kg","group_name":"label_7","department":"length_7_cm","class_name":"width_7_cm","sub_class":"height_7_cm","category_1":"diameter_7_cm","category_2":"size_7_ml","category_3":"weight_7_kg","category_4":"label_8","category_5":"length_8_cm","copy_writing_agency":"width_8_cm","photographer_name":"height_8_cm","photography_date":"diameter_8_cm","event":"size_8_ml","status":"weight_8_kg","style":"label_9","p_family":"length_9_cm","pf_name":"width_9_cm","feature_1":"height_9_cm","primary_material":"diameter_9_cm","secondary_material":"size_9_ml","type_name":"weight_9_kg","material_finish_1":"label_10","material_finish_2":"length_10_cm","for_name":"width_10_cm","gender":"height_10_cm","care_instructions":"diameter_10_cm","fragile":"size_10_ml","assembly_required":"weight_10_kg","unit_components":"segment"}" } 2536789array(2) { ["product_id"]=&gt; string(7) "2536789" ["product_attributes"]=&gt; string(1570) "{"cv_id":"","sv_id":"","collection_id":"","season":"JJ17","hit":"JJ17","tier":"TIER1","style_no":"11DD DV","base_product_id":"","product_title":"something","introduction":"","color_name":"blue","price_a":"169","color_price_a":"","sku_price_a":"","price_b":"189","color_price_b":"","price_c":"1.5","color_price_c":"","sku_price_c":"","product_availability_b":"","product_availability_c":"","product_availability_d":"","returnable_a":"","returnable_b":"","returnable_c":"","collect_a":"","collect_b":"","collect_c":"","brand":"brand a","group_name":"group b","department":"something else","class_name":"something","sub_class":"","category_1":"","category_2":"","category_3":"","category_4":"","category_5":"","copy_writing_agency":"","photographer_name":"","photography_date":"","event":"exclusive","status":"Active","style":"Basic","p_family":"something","pf_name":"something else","feature_1":"","primary_material":"Glass","secondary_material":"Glass","type_name":"something","material_finish_1":"something","material_finish_2":"","for_name":"","gender":"","care_instructions":"","fragile":"Yes","assembly_required":"","unit_components":"","unit_pack_length_cm":"24","unit_pack_width_cm":"16","unit_pack_height_cm":"32","unit_pack_weight_g":"","length_cm":"","width_cm":"","height_cm":"","weight_kg":"","size_ml":"250","diameter_cm":"","set_includes":"","label_1":"","length_1_cm":"","width_1_cm":"","height_1_cm":"","diameter_1_cm":"","size_1_ml":"","weight_1_kg":"","label_2":"","length_2_cm":"","width_2_cm":"","height_2_cm":"","diameter_2_cm":"","size_2_":"","":""}" }INSERT INTO process_detail (process_id,product_id,product_attributes) values(1,'2536789','[{"cv_id":"","sv_id":"","collection_id":"","season":"JJ17","hit":"JJ17","tier":"TIER1","style_no":"11DD DV","base_product_id":"","product_title":"something","introduction":"","color_name":"blue","price_a":"169","color_price_a":"","sku_price_a":"","price_b":"189","color_price_b":"","price_c":"1.5","color_price_c":"","sku_price_c":"","product_availability_b":"","product_availability_c":"","product_availability_d":"","returnable_a":"","returnable_b":"","returnable_c":"","collect_a":"","collect_b":"","collect_c":"","brand":"brand a","group_name":"group b","department":"something else","class_name":"something","sub_class":"","category_1":"","category_2":"","category_3":"","category_4":"","category_5":"","copy_writing_agency":"","photographer_name":"","photography_date":"","event":"exclusive","status":"Active","style":"Basic","p_family":"something","pf_name":"something else","feature_1":"","primary_material":"Glass","secondary_material":"Glass","type_name":"something","material_finish_1":"something","material_finish_2":"","for_name":"","gender":"","care_instructions":"","fragile":"Yes","assembly_required":"","unit_components":"","unit_pack_length_cm":"24","unit_pack_width_cm":"16","unit_pack_height_cm":"32","unit_pack_weight_g":"","length_cm":"","width_cm":"","height_cm":"","weight_kg":"","size_ml":"250","diameter_cm":"","set_includes":"","label_1":"","length_1_cm":"","width_1_cm":"","height_1_cm":"","diameter_1_cm":"","size_1_ml":"","weight_1_kg":"","label_2":"","length_2_cm":"","width_2_cm":"","height_2_cm":"","diameter_2_cm":"","size_2_":"","":""}]')

这是我的 csv 内容 product_id,cv_id,sv_id,collection_id,season,hit,tier,style_no,base_product_id,product_title,introduction,color_name,price_a,color_price_a,sku_price_a,price_b,color_price_b,price_c,color_price_c,sku_price_c,product_availability_b,product_availability_c,product_availability_d,returnable_a,returnable_b,returnable_c,collect_a,collect_b,collect_c,brand,group_name,department,class_name,sub_class,category_1,category_2,category_3,category_4,category_5,copy_writing_agency,photographer_name,photography_date,event,status,style,p_family,pf_name,feature_1,primary_material,secondary_material,type_name,material_finish_1,material_finish_2,for_name,gender,care_instructions,fragile,assembly_required,unit_components,unit_pack_length_cm,unit_pack_width_cm,unit_pack_height_cm,unit_pack_weight_g,length_cm,width_cm,height_cm,weight_kg,size_ml,diameter_cm,set_includes,label_1,length_1_cm,width_1_cm,height_1_cm,diameter_1_cm,size_1_ml,weight_1_kg,label_2,length_2_cm,width_2_cm,height_2_cm,diameter_2_cm,size_2_ml,weight_2_kg,label_3,length_3_cm,width_3_cm,height_3_cm,diameter_3_cm,size_3_ml,weight_3_kg,label_4,length_4_cm,width_4_cm,height_4_cm,diameter_4_cm,size_4_ml,weight_4_kg,label_5,length_5_cm,width_5_cm,height_5_cm,diameter_5_cm,size_5_ml,weight_5_kg,label_6,length_6_cm,width_6_cm,height_6_cm,diameter_6_cm,size_6_ml,weight_6_kg,label_7,length_7_cm,width_7_cm,height_7_cm,diameter_7_cm,size_7_ml,weight_7_kg,label_8,length_8_cm,width_8_cm,height_8_cm,diameter_8_cm,size_8_ml,weight_8_kg,label_9,length_9_cm,width_9_cm,height_9_cm,diameter_9_cm,size_9_ml,weight_9_kg,label_10,length_10_cm,width_10_cm,height_10_cm,diameter_10_cm,size_10_ml,weight_10_kg,segment 2536789,,,,JJ17,JJ17,TIER1,11DD DV,,something,,blue,169,,,189,,1.5,,,,,,,,,,,,brand a,group b,something else,something,,,,,,,,,,exclusive,Active,Basic,something,something else,,Glass,Glass,something,something,,,,,Yes,,,24,16,32,,,,,,250,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

提前致谢。

【问题讨论】:

  • 到目前为止你尝试过什么?你在哪里卡住了?有什么错误吗?
  • @Kathara,我在插入时遇到的挑战是,一些 CSV 文件将有 5 个属性列,有些将有 10 个属性,我在插入语句中不能有静态属性字段名称。
  • 如果您“插入到表中”,那么您应该规范化您的架构。
  • @symcbean- 好点,但假设 OP 可以控制数据库模式。
  • @symcbean,我的架构中没有任何数据重复

标签: php arrays json for-loop while-loop


【解决方案1】:

有几种方法可以解决这个问题,但这是我要解决的方法。

请注意,这是未经测试的代码,因此请进行故障排除或在 cmets 中询问是否存在问题/错误。

注意:fgetcsv 中的大小限制是可选的,不是必需的,在这种情况下会导致问题 - 因此请按照以下代码删除限制。

我已经评论了下面的代码,告诉你每一步发生了什么:

// declare an empty array to contain the column keys
$keys = [];

// load the keys into the $keys array
// load the first row
$data = fgetcsv( $source );
// loop over it, adding the keys into the array
foreach( $data AS $value ) {
    if ( $value ) {
        $keys[] = $value;
    }
}

/**
 * $keys should now be an array that looks like so:
 *
 * $keys = array(
 *             0 => 'product_id',
 *             1 => 'color',
 *             2 => 'style',
 *             3 => 'type',
 *             ..etc
 *         );
 */

// now process the rest of the csv file
while ($data = fgetcsv ( $source ) ) {
    // reset the attributes array, where we'll store the (future) JSON attributes
    $attributes = [];
    // reset product_id to ensure pick up the new / correct value
    $product_id = NULL;
    // loop over the columns in the row
    foreach( $data as $index => $value ) {
        // set the key (based on the keys array established above)
        $key = $keys[ $index ];

        // load the values into the attributes array (unless product id, where we put that in the $product_id variable)
        if ( 'product_id' != $key ) {
            $attributes[ $key ] = $value;
        } else {
            $product_id = $value;
        }
    }

    if ( empty( $attributes ) || ! $product_id ) {
        continue;
    }

    // prepare the values for a PDO database insert
    $bound_values = [
        'product_id'         => $product_id,
        'product_attributes' => json_encode( $attributes )
    ];

    // insert into the database
    $stmt = $db->prepare( "INSERT INTO product_table SET product_id = :product_id, product_attributes = :product_attributes" );
    $stmt->execute( $bound_values );
}

【讨论】:

  • 非常感谢,我正在尝试您的解决方案。很快就会更新
  • 我尝试了 10 列,但效果很好,但是当我尝试超过 30 列时,它又插入了一行。例如,我在一个文件中有两行超过 30 列,它正在插入 3 行,两行正确插入,一行错误插入,不同的标题不同的值。
  • 我的文件有时会有几个带有标题的空单元格,仅供您参考。
  • 空单元格让它插入带有空值的键
  • 谢谢你,我已经更新了你的更新。当我回显 $product_id; var_dump($bound_values);我将特定字段的标题名称作为product_id,但我有一个不同的表,其中包含用于测试的各个属性列,当我在该表中插入同一个文件时,相同的文件有效。
猜你喜欢
  • 1970-01-01
  • 2018-04-26
  • 2017-09-07
  • 2014-11-19
  • 1970-01-01
  • 1970-01-01
  • 2016-03-18
  • 2022-01-17
  • 1970-01-01
相关资源
最近更新 更多