CSV转PHP数组转Json文件
目的:把CSV数据转成JSON数据,方便前端直接调用
<?php $feiwangchaoqiuyuan = './csv/feiwangchaoqiuyuan.csv';//非王朝球员 $wangchaoqiuyuan = './csv/wangchaoqiuyuan.csv'; //王朝球员 $wangchaoqiuyuannengli = './csv/wangchaoqiuyuannengli.csv'; //王朝球员能力 $qiudui = './csv/qiudui.csv'; //球队 //'isDynasty': '0', 是否是王朝 $D_nengli = getData($wangchaoqiuyuannengli); writeToPHP(getData($feiwangchaoqiuyuan),'./php/feiwangchaoqiuyuan.php'); //生成PHP数组文件 非王朝球员 ID 为主键 writeToPHP(getData($wangchaoqiuyuan),'./php/wangchaoqiuyuan.php');//生成PHP数组文件 王朝球员 ID 为主键 writeToPHP(getData($feiwangchaoqiuyuan,3),'./php/feiwangchaoqiuyuan_cn.php');//生成PHP数组文件非王朝球员 中文名 为主键 writeToPHP(getData($wangchaoqiuyuan,3),'./php/wangchaoqiuyuan_cn.php');//生成PHP数组文件 王朝球员 中文名 为主键 writeToPHP(getData($wangchaoqiuyuannengli,0,true),'./php/wangchaoqiuyuannengli.php');//生成PHP数组文件 王朝球员能力 ID 为主键 writeToPHP(getData($qiudui),'./php/qiudui.php'); //生成PHP数组文件 球队 ID 为主键 //第2.1 生成 indexData.js文件 $file = 'js/indexData.js'; ob_start(); echo "var players = [\r\n"; foreach ($feiwangchaoqiuyuan as $key=>$value){ echo $tmp_str = <<<STR { 'playerID': '{$value['playerID']}', 'avartarID': '{$value['avartarID']}', 'quality': '{$value['quality']}', 'name': '{$value['name']}', 'nickname': '{$value['nickname']}', 'firstPosition': '{$value['firstPosition']}', 'secondPosition': '{$value['secondPosition']}', 'isDynasty': '0', 'number': '{$value['number']}', 'height': '{$value['height']}', 'weight': '{$value['weight']}', 'teamID': '{$value['teamID']}', 'rank': '{$value['rank']}', 'aHStar': '', //非王朝球员没有这个属性 'championTimes': '{$value['championTimes']}', 'allStarTimes': '{$value['allStarTimes']}', 'mvpTimes': '{$value['mvpTimes']}', 'scoreTimes': '{$value['scoreTimes']}', 'assistTimes': '{$value['assistTimes']}', 'reboundTimes': '{$value['reboundTimes']}' }, STR; } foreach ($wangchaoqiuyuan as $key=>$value){ echo $tmp_str = <<<STR { 'playerID': '{$value['playerID']}', 'avartarID': '{$value['avartarID']}', 'quality': '{$value['quality']}', 'name': '{$value['name']}', 'nickname': '{$value['nickname']}', 'firstPosition': '{$value['firstPosition']}', 'secondPosition': '{$value['secondPosition']}', 'isDynasty': '1', 'number': '{$value['number']}', 'height': '{$value['height']}', 'weight': '{$value['weight']}', 'teamID': '{$value['teamID']}', 'rank': '{$value['rank']}', 'aHStar': '{$value['isWork']}', 'championTimes': '{$value['championTimes']}', 'allStarTimes': '{$value['allStarTimes']}', 'mvpTimes': '{$value['mvpTimes']}', 'scoreTimes': '{$value['scoreTimes']}', 'assistTimes': '{$value['assistTimes']}', 'reboundTimes': '{$value['reboundTimes']}' }, STR; } echo "\r\n]"; $content = ob_get_contents(); ob_end_clean(); file_put_contents($file, $content); echo $content; function writeToPHP($data,$file){ if(strpos($file, '.php') === false) $file = $file.'.php'; file_exists($file) or touch($file); $str = var_export($data,TRUE); $str = "<?php\r\n return ".$str.";\r\n"; file_put_contents($file, $str); } function getData($file_name,$priKey=0,$recursion = false){ $baseDataHandle = fopen($file_name, "r"); $data = array(); $keys = fgetcsv($baseDataHandle); $keys = fgetcsv($baseDataHandle);//第二次是对应的键值 while (!feof($baseDataHandle)) { $tmp_value = fgetcsv($baseDataHandle); $tmp_data = array_combine($keys, $tmp_value); if($recursion){ $data[$tmp_value[$priKey]][] = $tmp_data; } else { $data[$tmp_value[$priKey]] = $tmp_data; } } fclose($baseDataHandle); return $data; }