Display non-adjacent data

Source: Internet
Author: User
Show non-adjacent data known $ arrs,
array (  0 =>   array (    'pono' => 'a',    'no' => '4711',  ),  1 =>   array (    'pono' => 'a',    'no' => '4714',  ),  2 =>   array (    'pono' => 'b',    'no' => '4714',  ),  3 =>   array (    'pono' => 'b',    'no' => '4715',  ),  4 =>   array (    'pono' => 'c',    'no' => '4715',  ),  5 =>   array (    'pono' => 'c',    'no' => '4716',  ),)


Merge the data with pono:
$ Tmp = array (); $ data = array (); $ I = 0; foreach ($ arrs as $ key => $ arr) {if (! Empty ($ tmp) & array_key_exists ($ arr ['Pono'], $ tmp) {$ key = $ tmp [$ arr ['Pono']; $ arr_no = explode (',', $ data [$ key] ['no']); if (! In_array ($ arr ['no'], $ arr_no) $ data [$ key] ['no']. = ','. $ arr ['no'];} else {$ tmp [$ arr ['Pono'] = $ I; $ data [$ I ++] = $ arr ;}} print_r ($ data); // The result is as follows: Array ([0] => Array ([pono] => a [no] => 4711,4714) [1] => Array ([pono] => B [no] => 4714,4715) [2] => Array ([pono] => c [no] => 4715,4716 ))


Now you want to display only non-adjacent no, as shown in the following example:
[0] => Array
(
[Pono] =>
[No] => 4711,4714
)

How can this problem be solved?


Reply to discussion (solution)

$arrs = array (  0 =>   array (    'pono' => 'a',    'no' => '4711',  ),  1 =>   array (    'pono' => 'a',    'no' => '4714',  ),  2 =>   array (    'pono' => 'b',    'no' => '4714',  ),  3 =>   array (    'pono' => 'b',    'no' => '4715',  ),  4 =>   array (    'pono' => 'c',    'no' => '4715',  ),  5 =>   array (    'pono' => 'c',    'no' => '4716',  ),);$tmp=array();$data=array();$i=0;foreach($arrs as $key=>$arr){        if(!empty($tmp) && array_key_exists($arr['pono'],$tmp)){        $key=$tmp[$arr['pono']];        $arr_no=explode(',',$data[$key]['no']);        if(!in_array($arr['no'],$arr_no)) $data[$key]['no'].=','.$arr['no'];    }else{        $tmp[$arr['pono']]=$i;        $data[$i++]=$arr;    }}print_r($data);$result = array();foreach($data as $val){list($a, $b) = explode(',', $val['no']);if(abs($a-$b)!=1){array_push($result, $val);}}print_r($result);


Array(    [0] => Array        (            [pono] => a            [no] => 4711,4714        ))



Thank you very much!

 Array ('Pono' => 'A', 'no' => '000000',), 1 => array ('Pono' => 'A ', 'No' => '123',), 2 => array ('Pono' => 'B', 'no' => '123 ',), 3 => array ('Pono' => 'B', 'no' => '000000',), 4 => array ('Pono' => 'C ', 'No' => '123',), 5 => array ('Pono' => 'C', 'no' => '123 ',), 6 => array ('Pono' => 'A', 'no' => '000000',), 7 => array ('Pono' => 'A ', 'No' => '123',), 8 => array ('Pono' => 'A', 'n' O '=> '200',),); $ tmp = array (); $ data = array (); $ I = 0; foreach ($ arrs as $ key => $ arr) {if (! Empty ($ tmp) & array_key_exists ($ arr ['Pono'], $ tmp) {$ key = $ tmp [$ arr ['Pono']; $ arr_no = explode (',', $ data [$ key] ['no']); if (! In_array ($ arr ['no'], $ arr_no) $ data [$ key] ['no']. = ','. $ arr ['no'];} else {$ tmp [$ arr ['Pono'] = $ I; $ data [$ I ++] = $ arr ;}} print_r ($ data); $ result = array (); foreach ($ data as $ val) {$ tmp = explode (',', $ val ['no']); if (! CheckSerialNum ($ tmp) {array_push ($ result, $ val) ;}} print_r ($ result); // Judge ???? Element ??? Word function checkSerialNum ($ arr) {asort ($ arr); $ arr = array_values ($ arr); for ($ I = 0, $ len = count ($ arr) -1; $ I <$ len; $ I ++) {if (abs ($ arr [$ I]-$ arr [$ I + 1])! = 1) {return false ;}} return true ;}?>

? After a while, 'No' can be supported without specifying 2 ?? Value ?.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.