Home >Backend Development >PHP Tutorial >数组比对并求得对应关系,该怎么解决

数组比对并求得对应关系,该怎么解决

WBOY
WBOYOriginal
2016-06-13 12:04:16978browse

数组比对并求得对应关系
已知数组a:

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


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

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


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

请问如何处理?
------解决方案--------------------

function foo($a, $b) {<br>  foreach($a as $v) {<br>    $c[] = array_search($v, $b);<br>  }<br>  for($i=1; $i<count></count>    if(abs($c[$i] - $c[$i-1]) != 1) {<br>      return '不连续';<br>    }<br>  }<br>  return '连续';<div class="clear">
                 
              
              
        
            </div>
Statement:
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