简体   繁体   English

如何使用PHP将原始文本文件数据转换为JSON?

[英]How to turn raw textfile data into json using PHP?

I was given a task to sort out data from a text file into JSON using PHP and object oriented principal. 我得到了一项任务,该任务是使用PHP和面向对象的主体将文本文件中的数据分类为JSON。 The text file has information and is displayed as follows (ignore #): 文本文件包含信息,并显示如下(忽略#):

#product_num:name:cost
5:tv:59.99
7:radio:10.99

I created a class and in this class I have one function that ignores any # or blank spaces from the data and puts the data in an array and json_encode it. 我创建了一个类,在这个类中,我有一个函数可以忽略数据中的任何#或空格,并将数据放入数组中并对其进行json_encode编码。 When I echo this data it looks like this: 当我回显这些数据时,它看起来像这样:

[{"1":"5:tv:59.99","2":"7:radio:10.99"}]

Is it possible to write other functions to separate the data further, for example so it looks more like: 是否可以编写其他函数来进一步分离数据,例如,这样看起来更像:

[{"product_number":"5","name":"tv","cost":"59.99"},{"product_number":"7","name":"radio","cost":"10.99"}]

If so can anyone give me any pointers and tips because I have no idea where or how to begin even after numerous google searches. 如果可以的话,任何人都可以给我任何指示和技巧,因为即使经过大量Google搜索,我也不知道从哪里开始或如何开始。

Your file format is basically a csv file and PHP has a CSV import function. 您的文件格式基本上是一个csv文件,而PHP具有CSV导入功能。 http://php.net/manual/en/function.fgetcsv.php http://php.net/manual/zh/function.fgetcsv.php

If you scroll down and check the CsvImporter example, you can copy/paste that and add a json_encode: 如果向下滚动并查看CsvImporter示例,则可以复制/粘贴该示例并添加json_encode:

$importer = new CsvImporter("small.txt",true,":"); 
$data = $importer->get(); 
echo json_encode($data); 

[{
    "\ufeff#product_num": "5",
    "name": "tv",
    "cost": "59.99"
},
{
    "\ufeff#product_num": "7",
    "name": "radio",
    "cost": "10.99"
}]

There is a function inside PHP for reading value separated lines, like CSV; PHP内有一个函数,用于读取值分隔的行,例如CSV; it's called fgetcsv which can also be used in object oriented PHP through SplFileObject::fgetcsv . 它称为fgetcsv ,也可以通过SplFileObject :: fgetcsv在面向对象的PHP中使用。

You will need to read each line, add the variable labels then append that to an array. 您将需要阅读每一行,添加变量标签,然后将其附加到数组中。

Depending on the size of the file you may need to optimize for memory usage as your array grows by saving as you proceed through the file. 根据文件的大小,您可能需要在进行文件操作时进行保存,以随着阵列的增长优化内存使用量。

<?php
$file = new SplFileObject('test.txt', 'r'); //Load the file
$file->current(); //Skip the first line (Side note: seek(1) did not appear to work while testing)

$result = [];
do { //While there are lines in the file
    list($product_num, $name, $cost) = $file->fgetcsv(':'); //Break the line on the : delimiter into an array and populate the array into the three named values
    $result[] = [ //Build the new json representation and add to result array
        'product_num' => $product_num,
        'name' => $name,
        'cost' => $cost,
    ];
} while (!$file->eof());

file_put_contents('output.json', json_encode($result)); //Save the result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM