我正在努力找出使其发挥作用的最佳方法。
我有一个很大的CSV文件,从一个非常旧的系统输出。我想输出一个json文件。 CSV中有几列。 “系列”列(第2列)是唯一标识符。并且'part#'与该'系列'代码相关联。
问题: 检查“系列”是否相同的最佳方法是什么,然后遍历所有匹配并将结果推送到“定价”数组?
[
{
"series": "01000",
"pricing": [{
"part_no": "010000.3",
"size": "0.30mm",
"stock": "5213",
"price2020": "4.55",
"price2017": "3.90",
"price2018": "0.65"
},
{
"part_no": "010000.35",
"size": "0.35mm",
"stock": "36225",
"price2020": "4.55",
"price2017": "3.90",
"price2018": "0.65"
},
{
"part_no": "010000.164",
"size": "1/64in",
"stock": "36225",
"price2020": "4.55",
"price2017": "3.90",
"price2018": "0.65"
}]
},
{
"series": "01001",
"pricing": [{
"part_no": "010001.3",
"size": "0.30mm",
"stock": "5213",
"price2020": "4.55",
"price2017": "3.90",
"price2018": "0.65"
},
{
"part_no": "010001.35",
"size": "0.35mm",
"stock": "36225",
"price2020": "4.55",
"price2017": "3.90",
"price2018": "0.65"
},
{
"part_no": "010001.164",
"size": "1/64in",
"stock": "36225",
"price2020": "4.55",
"price2017": "3.90",
"price2018": "0.65"
}]
}
]
"Part #","Series #","price0","price1","price2","price3","Size"
"010000.35","01000",£4.10,€ 3.32,€ 0.92,,"0.35mm","N",0.0138,
"01000-164","01000",£3.89,€ 2.89,€ 0.81,,"1/64""","N",0.0156,
"010000.4","01001",£3.88,€ 3.18,€ 0.65,,"0.40mm","N",0.0157,
"010000.45","01001",£3.66,€ 2.88,€ 0.81,,"0.45mm","N",0.0177,
"010000.5","01000",£2.93,€ 2.59,€ 0.62,,"0.50mm","N",0.0197,
我输出josn就好了,但我无法找出检查'series'是否相同的最佳方法,如果是,则遍历所有匹配并在数组中创建一个数组。
<?php
header('Content-type: application/json');
// Set your CSV feed
$feed = 'pricelist.csv';
// Arrays we'll use later
$keys = array();
$newArray = array();
// Function to convert CSV into associative array
function csvToArray($file, $delimiter) {
if (($handle = fopen($file, 'r')) !== FALSE) {
$i = 0;
while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) {
$part = $lineArray[0];
$series = $lineArray[1];
$janprice = $lineArray[2];
$septprice1 = $lineArray[3];
$septprice2 = $lineArray[4];
$size = $lineArray[5];
if ($series = $series){
for ($j = 0; $j < count($lineArray); $j++) {
$arr[$i][$j] = $lineArray[$j];
}
$i++;
}
}
fclose($handle);
}
return $arr;
}
// Do it
$data = csvToArray($feed, ',');
// Set number of elements (minus 1 because we shift off the first row)
$count = count($data) - 1;
//Use first row for names
$labels = array_shift($data);
foreach ($labels as $label) {
$keys[] = $label;
}
// Add Ids, just in case we want them later
$keys[] = 'id';
for ($i = 0; $i < $count; $i++) {
$data[$i][] = $i;
}
// Bring it all together
for ($j = 0; $j < $count; $j++) {
$d = array_combine($keys, $data[$j]);
$newArray[$j] = $d;
}
// Print it out as JSON
echo json_encode($newArray);
?>
答案 0 :(得分:1)
function csvToArray($file, $delimiter) {
if (($handle = fopen($file, 'r')) !== FALSE) {
$arr = array();
$final = array();
while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) {
$part = $lineArray[0];
$series = $lineArray[1];
$janprice = $lineArray[2];
$septprice1 = $lineArray[3];
$septprice2 = $lineArray[4];
$size = $lineArray[5];
if (!isset($arr[$series]))
{
$arr[$series] = array();
}
array_push($arr[$series], array("part_no" => $part, "size" => $size, ...));
}
foreach($arr as $serie => $data)
{
array_push($final, array(
"series" => $serie,
"pricing" => $data
));
}
fclose($handle);
}
return $final;
}