Rumah > pembangunan bahagian belakang > tutorial php > 显示不相邻的数据

显示不相邻的数据

WBOY
Lepaskan: 2016-06-23 13:52:20
asal
976 orang telah melayarinya

已知$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',  ),)
Salin selepas log masuk


现将同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);//得到结果:Array(    [0] => Array        (            [pono] => a            [no] => 4711,4714        )    [1] => Array        (            [pono] => b            [no] => 4714,4715        )    [2] => Array        (            [pono] => c            [no] => 4715,4716        ))
Salin selepas log masuk


现在想求仅显示不相邻的no,如例中的项:
[0] => Array
(
[pono] => a
[no] => 4711,4714
)

请问怎么求?


回复讨论(解决方案)

$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);
Salin selepas log masuk


Array(    [0] => Array        (            [pono] => a            [no] => 4711,4714        ))
Salin selepas log masuk



非常感谢!

<?php$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',  ),  6 =>   array (    'pono' => 'a',    'no' => '4712',  ),  7 =>   array (    'pono' => 'a',    'no' => '4713',  ),  8 =>   array (    'pono' => 'a',    'no' => '4719',  ),);$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);// 判????元素是否???字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;}?>
Salin selepas log masuk

?化了一下,可以支持'no'不指定2??值的情?。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan