首頁  >  文章  >  後端開發  >  数组比对并求得对应关系,该怎么解决

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

WBOY
WBOY原創
2016-06-13 12:04:16948瀏覽

数组比对并求得对应关系
已知数组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>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn