php文件去重复,二维数组筛选
2015-08-19 16:20
721 查看
http://www.porter.com/fr/fr/product/648162|Sneakers http://www.porter.com/fr/fr/product/642115|Boots http://www.porter.com/fr/fr/product/642115|Flat_Shoes http://www.porter.com/fr/fr/product/642115|Pumps http://www.porter.com/fr/fr/product/642115|Sandals http://www.porter.com/fr/fr/product/642115|Sneakers
-----------目标 将|左侧重复的变成---
http://www.porter.com/fr/fr/product/648162|Sneakers http://www.porter.com/fr/fr/product/642115|Boots@Flat_Shoes@Pumps@Sandals@Sneakers
=====================================================================
<?php
$strs = file("cat.txt");
$strs = array_map('trim',$strs);
$strsc = count($strs);
for($i=0;$i<$strsc;$i++)
{
$keyexplode[] = explode('|',$strs[$i]);
}
//$keyexplodet = array(array('184007', "Flat_Shoes" ), array('184007', "Pumps" ), array('184008', "Sandals" ));
function assoc_unique($arr, $key)
{
$tmp_arr = array();
foreach($arr as $k => $v)
{
if(in_array($v[$key], $tmp_arr))//搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true
{
//echo "<hr/>--";
//print_r($v[$key]);
//print_r($tmp_arr);echo "---<hr/>";
unset($arr[$k][0]);
}
else {
$tmp_arr[] = $v[$key];
}
}
//sort($arr); //sort函数对数组进行排序
return $arr;
}
//print_r("<pre/>");
//print_r(assoc_unique($keyexplode,0));
$keyexplodenew = assoc_unique($keyexplode,0);
//print_r($keyexplodenew);
for($j=0;$j<count($keyexplodenew);$j++)
{
// print_r($j);
// print_r($keyexplodenew[$j]);
if(isset($keyexplodenew[$j][0]))
{
echo "<br/>".$keyexplodenew[$j][0].'|';
//array_merge($keyexplodenew[$j], $keyexplodenew[$j-1]);
}
else{echo '@';}
echo $keyexplodenew[$j][1];
}
//print_r($keyexplodenew);
-----------目标 将|左侧重复的变成---
http://www.porter.com/fr/fr/product/648162|Sneakers http://www.porter.com/fr/fr/product/642115|Boots@Flat_Shoes@Pumps@Sandals@Sneakers
=====================================================================
<?php
$strs = file("cat.txt");
$strs = array_map('trim',$strs);
$strsc = count($strs);
for($i=0;$i<$strsc;$i++)
{
$keyexplode[] = explode('|',$strs[$i]);
}
//$keyexplodet = array(array('184007', "Flat_Shoes" ), array('184007', "Pumps" ), array('184008', "Sandals" ));
function assoc_unique($arr, $key)
{
$tmp_arr = array();
foreach($arr as $k => $v)
{
if(in_array($v[$key], $tmp_arr))//搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true
{
//echo "<hr/>--";
//print_r($v[$key]);
//print_r($tmp_arr);echo "---<hr/>";
unset($arr[$k][0]);
}
else {
$tmp_arr[] = $v[$key];
}
}
//sort($arr); //sort函数对数组进行排序
return $arr;
}
//print_r("<pre/>");
//print_r(assoc_unique($keyexplode,0));
$keyexplodenew = assoc_unique($keyexplode,0);
//print_r($keyexplodenew);
for($j=0;$j<count($keyexplodenew);$j++)
{
// print_r($j);
// print_r($keyexplodenew[$j]);
if(isset($keyexplodenew[$j][0]))
{
echo "<br/>".$keyexplodenew[$j][0].'|';
//array_merge($keyexplodenew[$j], $keyexplodenew[$j-1]);
}
else{echo '@';}
echo $keyexplodenew[$j][1];
}
//print_r($keyexplodenew);
相关文章推荐
- laravel-soap 使用
- php生成图片验证码-附五种验证码
- Ubuntu下搭建tftp服务器最简单方法
- js把预定义的html字符串转换为 HTML 实体 htmlspecialchars 输出html实体内容包括标签,而不自动转义标签,只显示内容,类似php的htmlspecialchars
- PHP 网页调用本地exe程序实例
- PHP设计模式之装饰模式
- PHP生命周期
- php基础语法
- 绘图: matplotlib核心剖析
- crontab 不能开启,且报错`/var/run.bak/crond.pid': Input/output error
- PHP设计模式之组合模式
- ThinkPHP自动验证分析
- root用户可以通过phpmyadmin登录,普通用户却不行,提示1045的错误
- ftp
- Mac下安装from matplotlib import pyplot或者 import matplotlib.pyplot as plt报错
- Yii 验证和消息
- iis7.5中使用fastcgi方式配置php5.6.5
- 解析PayPal支付接口的PHP开发方式
- PHPStorm单词拼写纠正提示设置
- zend framework分析总结[转]