【发布时间】:2017-01-30 12:39:10
【问题描述】:
此代码在 php 7 (localhost) 中有效,但在 php 5 (isp) 中无效。它将分隔的文本文件转换为json 文件以用于dataTables。
来源:
6590|07/19/2003|RCC Sat. Open|C11|Nikolayev, Igor (FM)|2402|Gonzalez, Jose|2131|1-0|
想要的结果:
{"data":[
{"game":"6624","Date":"11/01/2003","Event":"RCC Sat. Open","ECO":"C65","White":"Liman, Christ","WhiteElo":"1729","Black":"Nikolayev, Igor (FM)","BlackElo":"2408","Result":"0-1"},
....
]}
怎么会?
<?php
$text = file('games.txt'); $count = count($text); arsort($text);
$X = 0;
$fp = fopen("games.json", "w");
fwrite ($fp, "{"); fwrite ($fp, '"data":[');
foreach($text as $line) {$token = explode("|", $line);
$game = $token[0]; $date = $token[1]; $event = $token[2]; $eco = $token[3]; $white = $token[4];
$white_rating = $token[5]; $black = $token[6]; $black_rating = $token[7]; $result = $token[8];
fwrite ($fp, "\n");
fwrite ($fp,'{');
fwrite ($fp, '"game":"'.$game.'",');
fwrite ($fp, '"Date":"'.$date.'",');
fwrite ($fp, '"Event":"'.$event.'",');
fwrite ($fp, '"ECO":"'.$eco.'",');
fwrite ($fp, '"White":"'.$white.'",');
fwrite ($fp, '"WhiteElo":"'.$white_rating.'",');
fwrite ($fp, '"Black":"'.$black.'",');
fwrite ($fp, '"BlackElo":"'.$black_rating.'",');
fwrite ($fp, '"Result":"'.$result.'"');
$X++;
fwrite ($fp, '}');
if ( $X <= $count-1 ) {fwrite ($fp, ',');}
}
fwrite ($fp, "]");
fwrite ($fp, "}");
fclose($fp);
header('Location:pgn_assistant.php');
?>
【问题讨论】:
-
当
json_encode原生存在时,不建议创建自己的JSON转换方法。你还有什么错误吗? -
我没有收到任何错误。我会尽快调查 json_encode。
-
从表面上看,我没有看到任何与 PHP 5.6 不兼容的东西。您尝试读取和写入的文件的权限是否正确?或者它们是否在正确的位置?
标签: php json datatables php-5.3 php-7