我需要写一些ID#,来自" cccr_mems,json"到文件" USCF_ID.txt"在PHP中。我真的很想知道如何做到这一点。它将为数据交换和操纵开辟更多可能性。
cccr_mems.json php数组(~300条记录)
[
{
"Name": "Adeyemon, Murie",
"Rating": 1000,
"CCCR_Exp": "1000.10.10",
"Class": "E",
"ID": 16720664
},
{
"Name": "Ahmed, Jamshed",
"Rating": 1735,
"CCCR_Exp": "2018.10.18",
"Class": "B",
"ID": 12537964
}]
<?php //write IDs to file
$lines = file('cccr_mems.json') or exit("Unable to open file!");
$myfile = fopen("USCF_ID.txt", "w") or die("Unable to open file!");
foreach($lines as $x => $x_value) {
fwrite($lines.ID); //supposed to output $lines[ID]
fwrite "\n";
}
fclose($myfile);
?>
答案 0 :(得分:1)
您可以尝试使用file_get_contents()
加json_decode()
&amp; file_put_contents()
<?php
$json = file_get_contents('cccr_mems.json'); // Reads entire file to string
$array = json_decode($json, true); // Converts json to array
$id = [];
foreach($array as $value) {
$id[] = $value['ID']; // Get all ID
}
file_put_contents('USCF_ID.txt', explode(PHP_EOL, $id)); // Write string to file