欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

数组比对并求得对应关系

程序员文章站 2022-05-20 18:34:09
...
已知数组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',  ),);


有对应关系,例如 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',  ),);


问:当数组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;}
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

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

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


可能吧。我要求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


如果变成这样可解么?

我没做检查,只是直接输出到 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'];  });

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

....



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


如何判断数组$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

这个意思?

$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

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

这个意思?



//数组$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);

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

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



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

function foo($a, $b) {  foreach($a as $v) {    $c[] = array_search($v, $b);  }  for($i=1; $i连续连续不连续

推荐阅读