使用 PHP 数组将 CSV table 数据转换为 YAML 文件
Converting CSV table data into YAML file using PHP arrays
我被分配了以下 PHP Uni 任务:
1) Export the Trades/Crafts table to Excel
2) Make three columns in Excel: Trade-ID, Category-ID, Trade-Name and organize it
3) Export the table as a .CSV file (easier for PHP manipulation)
4) Write a PHP script that creates a YAML file from CSV file that corresponds to the structure of AppBundle / Resources / fixtures / prod / trades.yml
How you do that exactly, is up to you. You are free to use whatever method you prefer, but the YAML file has to have
the right structure.
这是 trades.yml 文件(作为示例供我们遵循):
-
ref: trade-1
id: 1
name: Plumber
category: $trade-category-1
-
ref: trade-16
id: 16
name: Electronic Engineer
category: $trade-category-2
您可能已经猜到了,我有一个 table 包含这 3 列(贸易 ID、类别 ID 和贸易名称)。 table 包含大约 150 行,其中包含许多不同类型的作业名称、作为类别 ID 的作业的 type/branch 以及作业本身的 ID。
我按照命令将 Excel table 导出为 .csv 文件。现在,我想我应该使用像 str_getcsv
或 fgetcsv
这样的函数,根据我的理解,它读取 CSV table 中的数据并将其转换为 PHP 数组.之后,我需要将这些数组转换为 YAML 语法格式,但我读到这并不是特别困难。
无论如何,YAML 结构(ref、id、名称、类别)中 'titles/entries' 的数量不等于 CSV 中的列数 table(商品名、猫 id、交易 ID),所以我不知道我应该如何或从哪里开始!
另外,对于 "ref" 部分,我想我必须让它看起来像这样: trade-<ID of trade>
,但是我怎样才能在数组中做类似 "trade-" . $TradeID
的事情呢?如何声明$TradeID = <ID of the job>
?我可以参考我想要的 CSV table 的行和列,例如 SQL 中的 SELECT
吗?或者我应该使用一个 WHILE 循环来获取 table 的所有行?
我试过这样:
<?php
$file = fopen('MyTable.csv', 'r') or die('error');
$line = array();
while (($line = fgetcsv($file)) !== FALSE) {
//$line is an array of the csv elements
// print_r($line);
foreach ($line as $key => $value) {
# code...
}
}
fclose($file);
但它输出的是这样的:
Array ( [0] => 1;1;Bituminiser; ) Array ( [0] => 1;2;Construction Dryer; ) Array ( [0] => 1;3;Concrete Driller and Cutter; ) Array ( [0] => 1;4;Concrete Block and Terrazzo Maker; ) Array ( [0] => 1;5;Well Builder; )
等等......但我还有另一个问题,加上每个数组中的[0]
。
实在想不通。如何使用 PHP 将 CSV table 转换为具有 trades.yml
结构的 YAML 文件?
试试这个:
fgetcsv($file, 0, ';')
中查找更多信息
但是,我为您制作了一个示例脚本:
<?php
$file = fopen('MyTable.csv', 'r') or die('error');
$yml = '';
$indent = str_repeat(' ', 4);
$keys = [
'ref',
'id',
'name',
'category',
];
while (($values = fgetcsv($file, 0, ';')) !== FALSE) {
$yml .= "-\n";
$arr = array_combine($keys, $values);
foreach ($arr as $key => $value) {
$yml .= "{$indent}{$key}: {$value}\n";
}
}
fclose($file);
echo $yml;
我被分配了以下 PHP Uni 任务:
1) Export the Trades/Crafts table to Excel
2) Make three columns in Excel: Trade-ID, Category-ID, Trade-Name and organize it
3) Export the table as a .CSV file (easier for PHP manipulation)
4) Write a PHP script that creates a YAML file from CSV file that corresponds to the structure of AppBundle / Resources / fixtures / prod / trades.ymlHow you do that exactly, is up to you. You are free to use whatever method you prefer, but the YAML file has to have the right structure.
这是 trades.yml 文件(作为示例供我们遵循):
-
ref: trade-1
id: 1
name: Plumber
category: $trade-category-1
-
ref: trade-16
id: 16
name: Electronic Engineer
category: $trade-category-2
您可能已经猜到了,我有一个 table 包含这 3 列(贸易 ID、类别 ID 和贸易名称)。 table 包含大约 150 行,其中包含许多不同类型的作业名称、作为类别 ID 的作业的 type/branch 以及作业本身的 ID。
我按照命令将 Excel table 导出为 .csv 文件。现在,我想我应该使用像 str_getcsv
或 fgetcsv
这样的函数,根据我的理解,它读取 CSV table 中的数据并将其转换为 PHP 数组.之后,我需要将这些数组转换为 YAML 语法格式,但我读到这并不是特别困难。
无论如何,YAML 结构(ref、id、名称、类别)中 'titles/entries' 的数量不等于 CSV 中的列数 table(商品名、猫 id、交易 ID),所以我不知道我应该如何或从哪里开始!
另外,对于 "ref" 部分,我想我必须让它看起来像这样: trade-<ID of trade>
,但是我怎样才能在数组中做类似 "trade-" . $TradeID
的事情呢?如何声明$TradeID = <ID of the job>
?我可以参考我想要的 CSV table 的行和列,例如 SQL 中的 SELECT
吗?或者我应该使用一个 WHILE 循环来获取 table 的所有行?
我试过这样:
<?php
$file = fopen('MyTable.csv', 'r') or die('error');
$line = array();
while (($line = fgetcsv($file)) !== FALSE) {
//$line is an array of the csv elements
// print_r($line);
foreach ($line as $key => $value) {
# code...
}
}
fclose($file);
但它输出的是这样的:
Array ( [0] => 1;1;Bituminiser; ) Array ( [0] => 1;2;Construction Dryer; ) Array ( [0] => 1;3;Concrete Driller and Cutter; ) Array ( [0] => 1;4;Concrete Block and Terrazzo Maker; ) Array ( [0] => 1;5;Well Builder; )
等等......但我还有另一个问题,加上每个数组中的[0]
。
实在想不通。如何使用 PHP 将 CSV table 转换为具有 trades.yml
结构的 YAML 文件?
试试这个:
fgetcsv($file, 0, ';')
中查找更多信息
但是,我为您制作了一个示例脚本:
<?php
$file = fopen('MyTable.csv', 'r') or die('error');
$yml = '';
$indent = str_repeat(' ', 4);
$keys = [
'ref',
'id',
'name',
'category',
];
while (($values = fgetcsv($file, 0, ';')) !== FALSE) {
$yml .= "-\n";
$arr = array_combine($keys, $values);
foreach ($arr as $key => $value) {
$yml .= "{$indent}{$key}: {$value}\n";
}
}
fclose($file);
echo $yml;