csv导入时如何在php中将少数列转换为JSON数组

csv导入时如何在php中将少数列转换为JSON数组,php,arrays,json,for-loop,while-loop,Php,Arrays,Json,For Loop,While Loop,我有一个csv文件,请看下面我的csv图像 这是我的桌子 我需要一个帮助来拆分几列并将其转换为数组并插入到表中,假设我想从数组中排除第一列“product_id”,并将其余所有列作为数组并插入到表中,因此预期的查询将类似于insert-into-process_-detail(product_-id,product_-attributes)值($product_-id,'[{“颜色”:“蓝色”,“款式”:“经典”,“材料类型”:“etc”,“长度”:“35厘米”,“价格”:“11.25”,“

我有一个csv文件,请看下面我的csv图像

这是我的桌子

我需要一个帮助来拆分几列并将其转换为数组并插入到表中,假设我想从数组中排除第一列“product_id”,并将其余所有列作为数组并插入到表中,因此预期的查询将类似于
insert-into-process_-detail(product_-id,product_-attributes)值($product_-id,'[{“颜色”:“蓝色”,“款式”:“经典”,“材料类型”:“etc”,“长度”:“35厘米”,“价格”:“11.25”,“产品说明”:“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
        }
函数转换($string)
{
返回htmlspecialchars($string,entu引号);
}
$columnArray=array();
$dataArray=array();
$firstRule=true;
而($data=fgetcsv($source,1000,“,”))
{
如果($firstRule)
{
foreach($columnName形式的数据)
{
$columnArray[]=$columnName;
}
$firstRule=false;
}
其他的
{
$rule=array();
对于($i=0;$i
echo$product\u id;var\u dump($bound\u values);

mlarray(2){[“产品id”]=>string(2)“ml”[“产品属性”]=>string(1588)”{“cv_id”:“重量2_kg”,“sv_id”:“标签3”,“集合id”:“长度3_cm”,“季节”:“宽度3_cm”,“命中率”:“高度3_cm”,“层数”:“直径3_cm”,“款式编号”:“尺寸3_ml”,“基础产品id”:“重量3_kg”,“产品名称”标签”,“长度4_”:,“颜色名称”:“宽度4厘米”,“价格a”:“高度4厘米”,“颜色价格a”:“直径4厘米”,“sku价格a”:“尺寸4厘米”,“价格b”:“重量4公斤”,“颜色价格b”:“标签5”,“价格c”:“长度5厘米”,“颜色价格c”:“宽度5厘米”,“sku价格c”:“高度5厘米”,“产品可用性5厘米”,“产品可用性5厘米”,“产品可用性5厘米”:“产品可用性”重量5公斤,可回收a:“标签6”,“可回收b:“长度6公斤”,“可回收c:“宽度6厘米”,“收集a:“高度6厘米”,“收集b:“直径6厘米”,“收集c:“尺寸6毫升”,“品牌”,“重量6公斤”,“组名”,“标签7”,“部门”,“长度7厘米”,“班级名称”,“宽度7厘米”,“亚级”高度7厘米,“类别1厘米”,“直径7厘米”,“类别7厘米”:“第三类”:“重量7公斤”,“第四类”:“标签8”,“第五类”:“长度8厘米”,“复印书写机构”:“宽度8厘米”,“摄影师姓名”:“高度8厘米”,“摄影日期”:“直径8厘米”,“事件”:“尺寸8毫升”,“状态”:“重量8公斤”,“风格”:“标签9”,“p系列”:“长度9厘米”,“pf姓名”:“宽度9厘米”,“特征1厘米”,“主要材料”:“直径9公分”、“二次材料”:“尺寸9公分”、“类型名称”:“重量9公分”、“材料表面处理1”:“标签10”、“材料表面处理2”:“长度10公分”、“材料名称”:“宽度10公分”、“性别”:“高度10公分”、“护理说明”:“直径10公分”、“易碎品”:“尺寸10公分”、“所需装配件”:“重量10公分”、“单元组件”:“段”}=>字符串(7)“2536789”[“产品属性”]=>字符串(1570)”{“cv_id”:“sv_id”:“收藏id”:“季节”:“JJ17”,“hit”:“JJ17”,“层次”:“第1层”,“样式编号”:“11DD DV”,“基本产品id”:“产品名称”:“某物”,“简介”:“颜色名称”:“蓝色”,“价格a”:“169”,“颜色价格a”:“sku价格a”:“价格b”:“189”,“颜色价格b”:”价格c:“1.5”、“颜色价格c:”“sku价格c:”“产品可用性b:”“产品可用性c:”“产品可用性d:”“可退货的a:”“可退货的b:”“可回收的a:”“可回收的a:”“可回收的b:”“可回收的b:”“可回收的b:”“可回收的a:”“可回收的b:”“可回收的b:”“可回收的a:”“品牌a”,“集团名称”:“b组”,“部门”:“其他东西”,“类别名称”:“某些东西”,“次级类别”:”“类别1”:“类别2”:“类别3”:“类别4”:“类别5”:“复印书写机构”:“摄影师姓名”:“摄影日期”:“事件”:“独家”、“状态”:“活动”、“风格”:“基本”、“p_系列”:“某物”、“pf名称”:“其他某物”、“特征1”:“主要材料”:“玻璃”、“次要材料”:“玻璃”、“类型名称”:“某物”、“某物”材料表面处理1:“某物”、“材料表面处理2:”用于名称“:”性别“:”护理说明“:”易碎品“:”是“,”需要装配“:”单位部件“:”单位包装长度“:”厘米“:”24“,”单位包装宽度“:”厘米“:”16“,”单位包装高度“:”厘米“:”32“,”单位包装重量“:”长度“:”宽度“:”厘米“,”高度“:”重量“:”公斤“,”尺寸“:”250“,”“直径1厘米”,“直径1厘米”,“尺寸1毫升”,“重量1千克”,“标签2厘米”,“长度2厘米”,“宽度2厘米”,“高度2厘米”,“直径2厘米”,“尺寸2厘米”,“尺寸2公斤”,“产品属性”中插入工艺id、工艺id、产品id、产品属性的详细信息[{“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:”“price_a:”“price_b:”“189”,“color_price_price_b:”“price_c:”“1.5”,“color_price_c:”“price:”“sku价格”:”“产品可用性”;“产品可用性”;“产品可用性”;“产品可用性”;“可回收”;“可回收”;“可回收”;“可回收”;“收集”;“收集”;“收集”;“品牌”;“品牌”;“品牌”;“集团”;“部门”;“其他物品”;“类别”;“物品”;“次级物品”;“类别”;“类别”;“类别”;“类别”;“类别”;“类别”;“类别”;“类别”;“类别4:“类别5:”复印/写作机构“:”摄影师姓名“:”摄影/日期“:”活动“:”独家“,”状态“,”活动“,”风格“,”基本“,”p家庭“:”某物“,”pf名称“:”其他“,”特征1“:”主要材料“:”玻璃“,”次要材料“:”玻璃“,”类型名称“:”某物“,”材料“:”表面处理“:”某物“,”
// 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 );
}