1,php从文本逐行读入数据,保存到数据组。使用fopen读取文本内容,逐行读取文本是$majorId = trim(fgets($rfile, 4096));。
$rfile = fopen("cate_id.txt", "r"); if ($rfile) { while (!feof($rfile)) { $majorId = trim(fgets($rfile, 4096)); $majorName = CategoryNamespace::getMajorCategoryById($majorId); if(!$majorName){ $majorName = CategoryNamespace::getMinorCategoryById($majorId); } $majorArray[$i]['id'] = $majorId; $majorArray[$i]['name'] = $majorName['name']; $i++; } fclose($rfile); }
2,将数组数导出到文本,将数组内容拆分为字符串,将字符串导出到文本。
$majorStr = "majorId"." "."majorName"." "; $wfile = 'cate_result.txt'; foreach($majorArray as $major){ $majorStr = $majorStr.trim($major['id'])." ".trim($major['name'])." "; } file_put_contents($wfile, $majorStr);