日期:2014-05-17 浏览次数:20538 次
$arr = array(1,2,2,2,3,4,5,5,5,6,2,2,7,8,2,5,3,9); $tmp = array(); foreach($arr AS $k => $v) { if(isset($tmp[$v])) { if($tmp[$v]) { $tmp[$v] .= ","; } $tmp[$v] .= $k; } else { $tmp[$v] = ""; } } foreach($tmp AS $k => $v) { if($v) { echo $k, "=>", $v, "\n"; } } unset($tmp);
------解决方案--------------------
$a = array( 1,2,2,2,3,4,5,5,5,6,2,2,7,8,2,5,3,9); $r = array(); foreach($a as $v) { if(isset($r[$v])) continue; if($t = array_keys(array_intersect($a, array($v)))) { unset($t[0]); $r[$v] = join(',', $t); } } $r = array_diff($r, array('')); print_r($r);
------解决方案--------------------
$arr = array(1,2,2,2,3,4,5,5,5,6,2,2,7,8,2,5,3,9); $str = implode(',', $arr); foreach ($arr as $k=>$v) { $t[$v] .= !isset($t[$v]) ? '' : $k . ','; $l += strlen($v); if((strrpos($str, $v.'')) == $l-strlen($v)) $t[$v] = trim($t[$v], ','); ++$l; } print_r($t);