Maison >développement back-end >tutoriel php >数组比对并求得对应关系
已知数组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', ),);
$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 = 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 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 列
你按你的需要检查一下不就行了?
至于打印次序,你调一下就是了
echo "\t\t" . join(',', $t); 是打印 lotnos 列的
数组 $t 中保有b数组中对应的 lotno
你排一下序,不就知道是否连续了吗?
echo "\t\t" . join(',', $t); 是打印 lotnos 列的
数组 $t 中保有b数组中对应的 lotno
你排一下序,不就知道是否连续了吗?
php 版本 5.2.5
echo "\t\t" . join(',', $t); 是打印 lotnos 列的
数组 $t 中保有b数组中对应的 lotno
你排一下序,不就知道是否连续了吗?
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 = 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 在条件语句之外,这是什么逻辑?
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));连续连续不连续