本帖最后由 lazygc520 于 2014-08-05 14:02:30 编辑 已知$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',
),
)
现将同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
)
)
现在想求仅显示不相邻的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']);
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
相关文章
相关视频
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论