search
HomeBackend DevelopmentPHP Tutorial数组处理,计算ng数量

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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.