Home  >  Article  >  Backend Development  >  数组处理,计算ng数量

数组处理,计算ng数量

WBOY
WBOYOriginal
2016-06-20 12:56:401099browse

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);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn