Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 数组处理,计算ng数量

数组处理,计算ng数量

Jun 20, 2016 pm 12:56 PM

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"}
Copy after login


已知数组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 />';							}
Copy after login

求得结果:
5106=>80015127=>72075127=>72075306=>70205306=>70205306=>70205306=>70205306=>70205306=>70205309=>71735309=>71735310=>71025310=>71025310=>72535310=>72535310=>74345310=>74345310=>74435310=>74435310=>75085310=>75085310=>75735310=>7573
Copy after login


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

$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 "
Copy after login
Copy after login
";/*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 "
Copy after login
Copy after login
";/*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));
Copy after login

得到如下结果:
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"}
Copy after login


想要得到的这些路径下对应的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
Copy after login
Copy after login

$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
Copy after login
Copy after login



如果有像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
Copy after login
Copy after login

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

$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
Copy after login
Copy after login



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

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

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1243
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

See all articles