21,887
社区成员
发帖
与我相关
我的任务
分享
<?php
$blocks = file("../test_file/test.txt");
$temp_array = array();
foreach($blocks as $key =>$value){
if(strpos($blocks[$key],'ICP')){
//取出每行的省份简称
$str = substr($blocks[$key],1,strpos($blocks[$key], 'ICP')-1);
}else {
$str = substr($blocks[$key],1,strpos($blocks[$key], 'B2')-1);
}
//为每个键取不同的键名
$temp_array[$str+$key] = $value;
unset($blocks[$key]);
}
array_multisort($temp_array,SORT_ASC,SORT_STRING);
foreach($temp_array as $key =>$value){
echo $key."-".$value."<br>";
}
$filename = "../test_file/test1.txt";
$file = fopen($filename, "w");
foreach($temp_array as $key =>$value){
$len=fwrite($file,$value);
}
fclose($file);
?>
<?php
$blocks = file("../test_file/test.txt");
$temp_array = array();
foreach($blocks as $key =>$value){
if(strpos($blocks[$key],'ICP')){
//取出每行的省份简称
$str = substr($blocks[$key],1,strpos($blocks[$key], 'ICP')-1);
}else {
$str = substr($blocks[$key],1,strpos($blocks[$key], 'B2')-1);
}
//把值当键,键当值
$temp_array[$value] = $str;
unset($blocks[$key]);
}
ksort($temp_array);
foreach($temp_array as $key =>$value){
echo $value."-".$key."<br>";
}
$filename = "../test_file/test2.txt";
$file = fopen($filename, "w");
foreach($temp_array as $key =>$value){
$len=fwrite($file,$key);
}
fclose($file);
?>