Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 数组比对并求得对应关系

数组比对并求得对应关系

Jun 23, 2016 pm 01:52 PM
relation array Comparison

已知数组a:

$a = array (  0 =>   array (    'packageno' => 'S2Y140805025',    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4212',  ),  1 =>   array (    'packageno' => 'S2Y140805025',    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4214',  ),  2 =>   array (    'packageno' => 'SYS140804073',    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4212',  ),  3 =>   array (    'packageno' => 'SYS140804073',    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4213',  ),  4 =>   array (    'packageno' => 'SYS140731064',    'type' => 'ECS1-713',    'cust_no' => '12657727',    'lotno' => '4154',  ),  5 =>   array (    'packageno' => 'SYS140731064',    'type' => 'ECS1-713',    'cust_no' => '12657727',    'lotno' => '4210',  ),  6 =>   array (    'packageno' => 'SYS140801090',    'type' => 'ECS1-713',    'cust_no' => '12657727',    'lotno' => '4200',  ),  7 =>   array (    'packageno' => 'SYS140801090',    'type' => 'ECS1-713',    'cust_no' => '12657727',    'lotno' => '4210',  ),);
Copy after login


有对应关系,例如 packageno S2Y140805025 对应 lotno 4212,4214(type为ECS0-235,cust_no为12654172时)

又知数组b:
$b = array (  0 =>   array (    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4211',  ),  1 =>   array (    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4212',  ),  2 =>   array (    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4213',  ),  3 =>   array (    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4214',  ),  4 =>   array (    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4216',  ),  5 =>   array (    'type' => 'ECS1-713',    'cust_no' => '12657727',    'lotno' => '4154',  ),  6 =>   array (    'type' => 'ECS1-713',    'cust_no' => '12657727',    'lotno' => '4200',  ),  7 =>   array (    'type' => 'ECS1-713',    'cust_no' => '12657727',    'lotno' => '4210',  ),);
Copy after login


问:当数组a和b的type和cust_no相同时,数组a中的packageno对应的lotno在数组b中是否连续,如果不连续,则将packageno显示出来。

请问如何处理?


回复讨论(解决方案)

你怎么总是有很奇怪的需求?可能你的流程有问题吧
都罗列出你看看

$a = array (  0 =>   array (    'packageno' => 'S2Y140805025',    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4212',  ),  1 =>   array (    'packageno' => 'S2Y140805025',    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4214',  ),  2 =>   array (    'packageno' => 'SYS140804073',    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4212',  ),  3 =>   array (    'packageno' => 'SYS140804073',    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4213',  ),  4 =>   array (    'packageno' => 'SYS140731064',    'type' => 'ECS1-713',    'cust_no' => '12657727',    'lotno' => '4154',  ),  5 =>   array (    'packageno' => 'SYS140731064',    'type' => 'ECS1-713',    'cust_no' => '12657727',    'lotno' => '4210',  ),  6 =>   array (    'packageno' => 'SYS140801090',    'type' => 'ECS1-713',    'cust_no' => '12657727',    'lotno' => '4200',  ),  7 =>   array (    'packageno' => 'SYS140801090',    'type' => 'ECS1-713',    'cust_no' => '12657727',    'lotno' => '4210',  ),);$b = array (  0 =>   array (    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4211',  ),  1 =>   array (    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4212',  ),  2 =>   array (    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4213',  ),  3 =>   array (    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4214',  ),  4 =>   array (    'type' => 'ECS0-235',    'cust_no' => '12654172',    'lotno' => '4216',  ),  5 =>   array (    'type' => 'ECS1-713',    'cust_no' => '12657727',    'lotno' => '4154',  ),  6 =>   array (    'type' => 'ECS1-713',    'cust_no' => '12657727',    'lotno' => '4200',  ),  7 =>   array (    'type' => 'ECS1-713',    'cust_no' => '12657727',    'lotno' => '4210',  ),);foreach($a as $i=>$r) {  if($i == 0) {    echo preg_replace("/\t/", "\t\t", join("\t", array_keys($r)), 1);    echo "\t\tlotnos", PHP_EOL;  }  echo join("\t", $r);  $t = array();  foreach(array_filter($b, function($v) use (&$r) {    return $v['type'] == $r['type'] && $v['cust_no'] == $r['cust_no'];  }) as $c) $t[] = $c['lotno'];  echo "\t\t" . join(',', $t);  echo PHP_EOL;}
Copy after login
packageno	   type	    cust_no	lotno		lotnosS2Y140805025	ECS0-235	12654172	4212		4211,4212,4213,4214,4216S2Y140805025	ECS0-235	12654172	4214		4211,4212,4213,4214,4216SYS140804073	ECS0-235	12654172	4212		4211,4212,4213,4214,4216SYS140804073	ECS0-235	12654172	4213		4211,4212,4213,4214,4216SYS140731064	ECS1-713	12657727	4154		4154,4200,4210SYS140731064	ECS1-713	12657727	4210		4154,4200,4210SYS140801090	ECS1-713	12657727	4200		4154,4200,4210SYS140801090	ECS1-713	12657727	4210		4154,4200,4210
Copy after login
Copy after login

你怎么总是有很奇怪的需求?可能你的流程有问题吧
都罗列出你看看

packageno	   type	    cust_no	lotno		lotnosS2Y140805025	ECS0-235	12654172	4212		4211,4212,4213,4214,4216S2Y140805025	ECS0-235	12654172	4214		4211,4212,4213,4214,4216SYS140804073	ECS0-235	12654172	4212		4211,4212,4213,4214,4216SYS140804073	ECS0-235	12654172	4213		4211,4212,4213,4214,4216SYS140731064	ECS1-713	12657727	4154		4154,4200,4210SYS140731064	ECS1-713	12657727	4210		4154,4200,4210SYS140801090	ECS1-713	12657727	4200		4154,4200,4210SYS140801090	ECS1-713	12657727	4210		4154,4200,4210
Copy after login
Copy after login



可能吧。我要求packageno对应的lotno是否按照数组b的顺序排列,像SYS140731064对应的lotno 4154,4210,在b数组中对应4154,4200,4210。4154和4210之间还有4200,所以认定SYS140731064排序异常,将其显示出来。

packageno	   type	    cust_no	lotno		lotnosS2Y140805025	ECS0-235	12654172	4212,4214		4211,4212,4213,4214,4216SYS140804073	ECS0-235	12654172	4212,4213		4211,4212,4213,4214,4216SYS140731064	ECS1-713	12657727	4154,4210		4154,4200,4210SYS140801090	ECS1-713	12657727	4200,4210		4154,4200,4210
Copy after login



如果变成这样可解么?

我没做检查,只是直接输出到 lotnos 列
你按你的需要检查一下不就行了?

至于打印次序,你调一下就是了

我没做检查,只是直接输出到 lotnos 列
你按你的需要检查一下不就行了?

至于打印次序,你调一下就是了



我不理解 lotno和lotnos之间如何比对。以第一个package,怎么判断其对应的lotno在lotnos中不连续?

echo "\t\t" . join(',', $t); 是打印 lotnos 列的
数组 $t 中保有b数组中对应的 lotno
你排一下序,不就知道是否连续了吗?

echo "\t\t" . join(',', $t); 是打印 lotnos 列的
数组 $t 中保有b数组中对应的 lotno
你排一下序,不就知道是否连续了吗?



1#代码编辑后运行不显示,
foreach(array_filter($b, function($v) use (&$r) {
return $v['type'] == $r['type'] && $v['cust_no'] == $r['cust_no'];
}) as $c) $t[] = $c['lotno'];
echo "\t\t" . join(',', $t);

php 版本 5.2.5

echo "\t\t" . join(',', $t); 是打印 lotnos 列的
数组 $t 中保有b数组中对应的 lotno
你排一下序,不就知道是否连续了吗?



貌似php 5.2.5不支持 function use这样的用法。
关于排序的用法,不能理解这句话的含义,麻烦还是举个例子。

  array_filter($b, function($v) use (&$r) {    return $v['type'] == $r['type'] && $v['cust_no'] == $r['cust_no'];  });
Copy after login

  array_filter($b, 'back');{  function back($v) {     global $r;     return $v['type'] == $r['type'] && $v['cust_no'] == $r['cust_no'];  }
Copy after login

....



换个形式问吧,
//假设数组$a,$a = array (  0 => '4154',  1 => '4210',);//数组$b,$b = array (  0 => '4154',  1 => '4200',  2 => '4210',);
Copy after login


如何判断数组$a是否在数组$b中连续?

这个意思?

$a = array (  0 => '4154',  1 => '4210',);$b = array (  0 => '4154',  1 => '4200',  2 => '4210',);foreach($a as $v) {  $c[] = array_search($v, $b);}for($i=1; $i<count($c); $i++) {  if(abs($c[$i] - $c[$i-1]) == 1) {    echo '不连续';    break;  }}
Copy after login

这个意思?

$a = array (  0 => '4154',  1 => '4210',);$b = array (  0 => '4154',  1 => '4200',  2 => '4210',);foreach($a as $v) {  $c[] = array_search($v, $b);}for($i=1; $i<count($c); $i++) {  if(abs($c[$i] - $c[$i-1]) == 1) {    echo '连续';  }  else  {	echo '不连续';  }  break;} 
Copy after login



但是这种情况应该是不连续,但是会变成连续。
$a = array (  0 => '4154',  1 => '4200',  2 => '4217',);$b = array (  0 => '4154',  1 => '4200',  2 => '4210',  3 => '4217',);//其中0,1连续,但是4217那项的键值和$b的4217的键值不等,所以判定为不连续。
Copy after login

这个意思?



//数组$a的值在数组$b中连续出现,判定为连续//例1:(连续)$a = array(4154,4200); $b = array(4154,4200,4210); //例2:(连续)$a = array(4200,4210);$b = array(4154,4200,4210,4217);//例3:(不连续)$a = array(4200,4217);$b = array(4154,4200,4210,4217);
Copy after login

判定不连续时才要 break 退出循环,因为再比较下去已经没有意义了
你的 break 在条件语句之外,这是什么逻辑?

判定不连续时才要 break 退出循环,因为再比较下去已经没有意义了
你的 break 在条件语句之外,这是什么逻辑?



请忽略13#,查看14#说明。

function foo($a, $b) {  foreach($a as $v) {    $c[] = array_search($v, $b);  }  for($i=1; $i<count($c); $i++) {    if(abs($c[$i] - $c[$i-1]) != 1) {      return '不连续';    }  }  return '连续';}echo foo(array(4154,4200), array(4154,4200,4210));echo foo(array(4200,4210), array(4154,4200,4210,4217));echo foo(array(4200,4217), array(4154,4200,4210,4217));
Copy after login
连续连续不连续

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to remove duplicate elements from PHP array using foreach loop? How to remove duplicate elements from PHP array using foreach loop? Apr 27, 2024 am 11:33 AM

The method of using a foreach loop to remove duplicate elements from a PHP array is as follows: traverse the array, and if the element already exists and the current position is not the first occurrence, delete it. For example, if there are duplicate records in the database query results, you can use this method to remove them and obtain results without duplicate records.

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

PHP array multi-dimensional sorting practice: from simple to complex scenarios PHP array multi-dimensional sorting practice: from simple to complex scenarios Apr 29, 2024 pm 09:12 PM

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy May 01, 2024 pm 12:30 PM

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Apr 30, 2024 pm 03:42 PM

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.

PHP array merging and deduplication algorithm: parallel solution PHP array merging and deduplication algorithm: parallel solution Apr 18, 2024 pm 02:30 PM

The PHP array merging and deduplication algorithm provides a parallel solution, dividing the original array into small blocks for parallel processing, and the main process merges the results of the blocks to deduplicate. Algorithmic steps: Split the original array into equally allocated small blocks. Process each block for deduplication in parallel. Merge block results and deduplicate again.

See all articles