数组处理,计算ng数量

WBOY
发布: 2016-06-20 12:56:40
原创
1077 人浏览过

array(6) {  [0]=>  string(62) "../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020.csv"  [1]=>  string(64) "../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_1.csv"  [2]=>  string(64) "../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_2.csv"  [3]=>  string(64) "../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_3.csv"  [4]=>  string(64) "../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_4.csv"  [5]=>  string(65) "../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_NG.csv"}
登录后复制


已知数组a的形式,如果把7020开头的文件看作一个文件,求得这样的结果:ts407,bed410-500a1,5306,ng对应数量为1.这要怎么求?


回复讨论(解决方案)

如果是这样呢?

foreach (glob($datapath."/NG/*.csv") as $filename) {								preg_match('/.+\/(\d+)/',$filename,$match);								echo $value_lotno.'=>'.$match[1].'<br />';							}
登录后复制

求得结果:
5106=>80015127=>72075127=>72075306=>70205306=>70205306=>70205306=>70205306=>70205306=>70205309=>71735309=>71735310=>71025310=>71025310=>72535310=>72535310=>74345310=>74345310=>74435310=>74435310=>75085310=>75085310=>75735310=>7573
登录后复制


如何把这个结果变成一个数组,像这样的形式:
array('5106=>8001','5127=>7207','5306=>7020','5309=>7173','5310=>7102','5310=>7253','5310=>7434','5310=>7443','5310=>7508','5310=>7573');
登录后复制

$arr=array(	'5106=>8001',	'5127=>7207',	'5127=>7207',	'5306=>7020',	'5306=>7020',	'5306=>7020',	'5306=>7020',	'5306=>7020',	'5306=>7020',	'5309=>7173',	'5309=>7173',	'5310=>7102',	'5310=>7102',	'5310=>7253',	'5310=>7253',	'5310=>7434',	'5310=>7434',	'5310=>7443',	'5310=>7443',	'5310=>7508',	'5310=>7508',	'5310=>7573',	'5310=>7573',);echo "<pre class="brush:php;toolbar:false">";print_r(array_values(array_unique($arr)));echo "
登录后复制
登录后复制
";/*Array( [0] => 5106=>8001 [1] => 5127=>7207 [2] => 5306=>7020 [3] => 5309=>7173 [4] => 5310=>7102 [5] => 5310=>7253 [6] => 5310=>7434 [7] => 5310=>7443 [8] => 5310=>7508 [9] => 5310=>7573)*/

$arr=array(	'5106=>8001',	'5127=>7207',	'5127=>7207',	'5306=>7020',	'5306=>7020',	'5306=>7020',	'5306=>7020',	'5306=>7020',	'5306=>7020',	'5309=>7173',	'5309=>7173',	'5310=>7102',	'5310=>7102',	'5310=>7253',	'5310=>7253',	'5310=>7434',	'5310=>7434',	'5310=>7443',	'5310=>7443',	'5310=>7508',	'5310=>7508',	'5310=>7573',	'5310=>7573',);echo "<pre class="brush:php;toolbar:false">";print_r(array_values(array_unique($arr)));echo "
登录后复制
登录后复制
";/*Array( [0] => 5106=>8001 [1] => 5127=>7207 [2] => 5306=>7020 [3] => 5309=>7173 [4] => 5310=>7102 [5] => 5310=>7253 [6] => 5310=>7434 [7] => 5310=>7443 [8] => 5310=>7508 [9] => 5310=>7573)*/



不对吧。原来的格式不是数组啊,我是要把原来的格式变成数组啊。

你不都循环出来了吗.....

echo $value_lotno.'=>'.$match[1].'
';
下面加上
$arr[]=$value_lotno.'=>'.$match[1];
这样就行了

你不都循环出来了吗.....

echo $value_lotno.'=>'.$match[1].'
';
下面加上
$arr[]=$value_lotno.'=>'.$match[1];
这样就行了


是我的问题,应该放在循环外面print_r,但是这问题问的已经和我的本意相去甚远了,我本来想问的是,如1#的路径,把路径最后的7020开头的文件都作为1个文件来看,计算出ng文件夹下的文件数量为1,这个要怎么求?

举个例子来看!

举个例子来看!


$datapath = '../../dat/DIG/TestFunction/'.$machine.'/'.$value_type.'/'.$value_lotno;//已求得var_dump(glob($datapath.'/NG/*.*', GLOB_BRACE));
登录后复制

得到如下结果:
array(1) {  [0]=>  string(62) "../../dat/DIG/TestFunction/TS407/BED410-500A1/5106/NG/8001.csv"}array(2) {  [0]=>  string(62) "../../dat/DIG/TestFunction/TS407/BED410-500A1/5127/NG/7207.csv"  [1]=>  string(65) "../../dat/DIG/TestFunction/TS407/BED410-500A1/5127/NG/7207_NG.csv"}array(0) {}array(0) {}array(6) {  [0]=>  string(62) "../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020.csv"  [1]=>  string(64) "../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_1.csv"  [2]=>  string(64) "../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_2.csv"  [3]=>  string(64) "../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_3.csv"  [4]=>  string(64) "../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_4.csv"  [5]=>  string(65) "../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_NG.csv"}
登录后复制


想要得到的这些路径下对应的NG的结果数量。(如果开头都一样的话则作为一个文件)

$arr=array(	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_1.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_2.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_3.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_4.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_NG.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7021_NG.csv",//添加测试);foreach($arr as $v){	$tmp=explode('/',$v);	$lastfile=array_pop($tmp);	preg_match('/^(\d+)(\.|\_)/',$lastfile,$m);	$arr_file[]=$m[1];}$count=count(array_unique($arr_file));echo $count;//2
登录后复制
登录后复制

$arr=array(	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_1.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_2.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_3.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_4.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_NG.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7021_NG.csv",//添加测试);foreach($arr as $v){	$tmp=explode('/',$v);	$lastfile=array_pop($tmp);	preg_match('/^(\d+)(\.|\_)/',$lastfile,$m);	$arr_file[]=$m[1];}$count=count(array_unique($arr_file));echo $count;//2
登录后复制
登录后复制



如果有像7#的空数组这样就不对了,变成了1.

唉,二维你就判断一下就行了嘛

$all=array(array(	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5106/NG/8001.csv"),array(	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5127/NG/7207.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5127/NG/7207_NG.csv",),array(),array(),array(	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_1.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_2.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_3.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_4.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_NG.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7021_NG.csv",//添加测试));foreach($all as $arr){	if(!$arr){		continue;	}	foreach($arr as $v){		$tmp=explode('/',$v);		$lastfile=array_pop($tmp);		preg_match('/^(\d+)(\.|\_)/',$lastfile,$m);		$arr_file[]=$m[1];	}}$count=count(array_unique($arr_file));echo $count;//4
登录后复制
登录后复制

唉,二维你就判断一下就行了嘛

$all=array(array(	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5106/NG/8001.csv"),array(	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5127/NG/7207.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5127/NG/7207_NG.csv",),array(),array(),array(	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_1.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_2.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_3.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_4.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7020_NG.csv",	"../../dat/DIG/TestFunction/TS407/BED410-500A1/5306/NG/7021_NG.csv",//添加测试));foreach($all as $arr){	if(!$arr){		continue;	}	foreach($arr as $v){		$tmp=explode('/',$v);		$lastfile=array_pop($tmp);		preg_match('/^(\d+)(\.|\_)/',$lastfile,$m);		$arr_file[]=$m[1];	}}$count=count(array_unique($arr_file));echo $count;//4
登录后复制
登录后复制



我不要求总和,求分项的和:1,1,2
请问怎么求?

我是醉了,能一次说完么,哥
$count=count(array_unique($arr_file));
改为
$new=array_count_values($arr_file);

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!