循环那部分一直写不对要求如下读取a.txt内容是111,2222333,4444555,7777这样的格式如果$goods_id的值等于其中一行前面的值那么转换成后面的输出,比如$goods_id=111,那么进行替换后输出$goods_id=2222
2 回答
慕神8447489
TA贡献1780条经验 获得超1个赞
$goods_id=111;$handle=fopen("f:/test.txt","r");while(!feof($handle)){$line=fgets($handle,4096);//echo$line."
";$str=explode(",",$line);if($str[0]==$goods_id){echo$str[1];}}fclose($handle);
茅侃侃
TA贡献1842条经验 获得超21个赞
`functiongetGoodsIpMap(){$fh=fopen('a.txt','r');$goodsMap=array();while($line=fgets($fh)!==false){if(empty($line)){continue;}list($oldId,$newId)=explode(',',trim($line));$goodsMap[$oldId]=$newId;}fclose($fh);}$goodsIdMap=getGoodsIpMap();$newId=isset($goodsIdMap[$goodsId])?$goodsIdMap[$goodsId]:$goodsId;`
添加回答
举报
0/150
提交
取消