本帖最后由 lazygc520 于 2014-08-05 14:02:30 编辑 已知$arrs,
<br />array (<br /> 0 => <br /> array (<br /> 'pono' => 'a',<br /> 'no' => '4711',<br /> ),<br /> 1 => <br /> array (<br /> 'pono' => 'a',<br /> 'no' => '4714',<br /> ),<br /> 2 => <br /> array (<br /> 'pono' => 'b',<br /> 'no' => '4714',<br /> ),<br /> 3 => <br /> array (<br /> 'pono' => 'b',<br /> 'no' => '4715',<br /> ),<br /> 4 => <br /> array (<br /> 'pono' => 'c',<br /> 'no' => '4715',<br /> ),<br /> 5 => <br /> array (<br /> 'pono' => 'c',<br /> 'no' => '4716',<br /> ),<br />)<br />
登入後複製
现将同pono的数据合并,得:
<br />$tmp=array();<br />$data=array();<br />$i=0;<br />foreach($arrs as $key=>$arr){<br /> if(!empty($tmp) && array_key_exists($arr['pono'],$tmp)){<br /> $key=$tmp[$arr['pono']];<br /> $arr_no=explode(',',$data[$key]['no']);<br /> if(!in_array($arr['no'],$arr_no)) $data[$key]['no'].=','.$arr['no'];<br /> }else{<br /> $tmp[$arr['pono']]=$i;<br /> $data[$i++]=$arr;<br /> }<br />}<br />print_r($data);<br /><br />//得到结果:<br />Array<br />(<br /> [0] => Array<br /> (<br /> [pono] => a<br /> [no] => 4711,4714<br /> )<br /><br /> [1] => Array<br /> (<br /> [pono] => b<br /> [no] => 4714,4715<br /> )<br /><br /> [2] => Array<br /> (<br /> [pono] => c<br /> [no] => 4715,4716<br /> )<br />)<br />
登入後複製
现在想求仅显示不相邻的no,如例中的项:
[0] => Array
(
[pono] => a
[no] => 4711,4714
)
请问怎么求?
------解决方案--------------------<br>$arrs = array (<br> 0 => <br> array (<br> 'pono' => 'a',<br> 'no' => '4711',<br> ),<br> 1 => <br> array (<br> 'pono' => 'a',<br> 'no' => '4714',<br> ),<br> 2 => <br> array (<br> 'pono' => 'b',<br> 'no' => '4714',<br> ),<br> 3 => <br> array (<br> 'pono' => 'b',<br> 'no' => '4715',<br> ),<br> 4 => <br> array (<br> 'pono' => 'c',<br> 'no' => '4715',<br> ),<br> 5 => <br> array (<br> 'pono' => 'c',<br> 'no' => '4716',<br> ),<br>);<br><br><br>$tmp=array();<br>$data=array();<br>$i=0;<br>foreach($arrs as $key=>$arr){<br> if(!empty($tmp) && array_key_exists($arr['pono'],$tmp)){<br> $key=$tmp[$arr['pono']];<br> $arr_no=explode(',',$data[$key]['no']);<br> if(!in_array($arr['no'],$arr_no)) $data[$key]['no'].=','.$arr['no'];<br> }else{<br> $tmp[$arr['pono']]=$i;<br> $data[$i++]=$arr;<br> }<br>}<br>print_r($data);<br><br>$result = array();<br><br>foreach($data as $val){<br> list($a, $b) = explode(',', $val['no']);<div class="clear">
</div>
登入後複製