首页 >后端开发 >php教程 >数组比对并求得对应关系

数组比对并求得对应关系

WBOY
WBOY原创
2016-06-23 13:52:09735浏览

已知数组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<count($c); $i++) {  if(abs($c[$i] - $c[$i-1]) == 1) {    echo '不连续';    break;  }}

这个意思?

$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;} 



但是这种情况应该是不连续,但是会变成连续。
$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<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));
连续连续不连续

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn