function unique_arr($array2D,$stkeep=false,$ndformat=true) { // 判斷是否保留一級數(shù)組鍵 (一級數(shù)組鍵可以為非數(shù)字) if($stkeep) $stArr = array_keys($array2D); // 判斷是否保留二級數(shù)組鍵 (所有二級數(shù)組鍵必須相同) if($ndformat) $ndArr = array_keys(end($array2D)); //降維,也可以用implode,將一維數(shù)組轉(zhuǎn)換為用逗號連接的字符串 foreach ($array2D as $v){ $v = join(",",$v); $temp[] = $v; } //去掉重復(fù)的字符串,也就是重復(fù)的一維數(shù)組 $temp = array_unique($temp); //再將拆開的數(shù)組重新組裝 foreach ($temp as $k => $v) { if($stkeep) $k = $stArr[$k]; if($ndformat) { $tempArr = explode(",",$v); foreach($tempArr as $ndkey => $ndval) $output[$k][$ndArr[$ndkey]] = $ndval; } else $output[$k] = explode(",",$v); } return $output; } |
|