我创建了一个方法,它将读取一个带有application / octet类型的文件,这里有一些代码。 原始数据:
GTHHS;MEKID Interface;5496;2012-07-20;
NM1;081;IN1;980898989;2001-01-15;Mr;Gaf;Uhkil;Uhkil,Gaf;PRI;Gaf
$contents = file_get_contents($tmp_filename);
$stringContents = explode(";", $contents);
现在它给了我这个输出:
Array
(
[0] => GTHHS
[1] => MEKID Interface
[2] => 5496
[3] => 2012-07-20
NM1
[4] => 081
[5] => IN1
[6] => 980898989
[7] => 2001-01-15
[8] => Mr
[9] => Gaf
[10] => Uhkil
[11] => Uhkil,Gaf
[12] => PRI
[13] => Gaf
PR1
[14] => 081
[15] => IN1
[16] => 20730089
[17] => 7 The Schooner
[18] => Auhaas
[19] => Huuula Ave
[20] =>
[21] => Kishma
PR2
[22] => 081
[23] => IN1
[24] => 232323233
[25] => 400006
[26] => HGD
[27] => M
[28] => M
[29] => 2007-10-16
[30] => 1976-03-31
);
如何将NM1
,PR1
作为数组的头部,如下所示:
Array (
[NM1] = array(
[0] => GTHHS
[1] => MEKID Interface
[2] => 5496
[3] => 2012-07-20
)
);
我还计划将内部数组[0] - [3]设为json。
答案 0 :(得分:1)
如果您按\n
分解内容,则每行都以该标识符开头。如果你只是在该行中;
爆炸并将其添加为子数组,那么就可以得到它。
这实际上看起来像一个普通的旧CSV文件,第一行带有ifentifier。如果是这样,尝试这样的事情:
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, ";", "\"", "\n")) !== FALSE)
{
$key = array_shift($row);
$data[$key] = $row;
}
fclose($handle);
}
echo json_encode($data);