csv - Prestashop 1.7 - 导入 csv(products) 的脚本,其中的列具有用逗号分隔的多个值
问题描述
我正在尝试能够导入具有以下结构的 csv:
name,reference,ean13,wholesale_price,price,quantity,category,manufacturer
Samsung Galaxy A1,259304,7076963846220,380.50,410,20,3,Samsung
Xiaomi MI Note 9,334573,23039239536421,499,530,30,1,Xiaomi
关键是它必须像我提议的那样,不可能以任何其他方式做到这一点,因为 csv 有数千条记录,大约有 20 条。
<?php
header("Content-Type: text/html;charset=utf-8");
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if (!defined('_PS_ADMIN_DIR_'))
define('_PS_ADMIN_DIR_', getcwd());
include(_PS_ADMIN_DIR_.'/../config/config.inc.php');
include(_PS_ADMIN_DIR_.'/functions.php');
include_once './tabs/AdminImport.php';
require_once(_PS_ADMIN_DIR_.'/init.php');
$context = Context::getContext();
$import = New AdminImportController();
loadProductsPost();
$import->productImport();
function loadProductsPost() {
$_POST = array (
'tab' => 'AdminImport',
'forceIDs' => '1',
'skip' => '1',
'csv' => 'products.csv',
'entity' => '1',
'separator' => ',',
'multiple_value_separator' => ',',
'iso_lang' => 'es',
'convert' => '',
'import' => '1',
"type_value" => array(
0 => 'name',
1 => 'reference',
2 => 'ean13',
3 => 'wholesale_price',
4 => 'price',
3 => 'quantity',
4 => 'category',
15 => 'manufacturer',
),
);
}
Tools::clearSmartyCache();
Tools::clearXMLCache();
Media::clearCache();
Tools::generateIndex();
?>
我已将此脚本作为参考,但在“type_value”部分中,数据似乎是按列获取的,我修改了数组,以便将第一列中的数据按部分获取,但将它们导出为空,我也试过这个功能,但没有:
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;
}
解决方案
推荐阅读
- azure - 使用 Azure b2c OpenID Connect 配置 Ckan 的身份验证
- javascript - 在没有数据层的电子商务上跟踪 GTM 上的转化价值
- visual-c++ - Visual C++ 函数调用显式/隐式参数计数
- python - 在 RMD 中运行 Python 块,类似于 Jupyter Notebook
- stripe-payments - 除第一个使用测试帐户外,条带订阅失败
- html - 无法将数组数据修补到反应形式中的素数表
- python - Pytorch 网络未训练
- c# - 元素点击被拦截并等待
- python - 如何仅从 Dataframe Pandas 中提取收盘价
- javascript - 解析名称值对的字符串