/***** Filter inclusion. The current keyword appears later, and the keyword is deleted later. * The keyword is left short and not long * @ param $ arr = array (); * @ return $ newArr = array (); * @ update 2013/9/24 */function arrFilter (& $ arr) {$ newArr = & $ arr; $ count = count ($ newArr ); for ($ I = 0; $ I <$ count; $ I ++) {for ($ k = $ count-1; $ k> $ I; $ k --) {$ needle = $ newArr [$ I]; $ haystack = $ newArr [$ k]; if (! $ Needle) continue; $ rst = substr ($ haystack, 0, strlen ($ needle) = $ needle? 1:0; if ($ rst) {array_splice ($ newArr, $ k, 1); array_values ($ newArr); $ count --;};}}} // After optimization, the algorithm function arrFilter2 (& $ arr) {sort ($ arr); foreach ($ arr as $ v1) {while ($ v2 = current ($ arr )) {$ l1 = strlen ($ v1); $ l2 = strlen ($ v2); if ($ l1> $ l2 or $ v1! = Substr ($ v2, 0, $ l1) break; $ k = key ($ arr); unset ($ arr [$ k]) ;}}
// Function cmpStr ($ str1, $ str2) {$ a = strlen ($ str1); $ B = strlen ($ str2 ); if ($ a = $ B) {if ($ str1 = $ str2) {return 0;} else {return ($ str1 <$ str2 )? -1: 1;} // return 0;} return ($ a <$ B )? -1: 1 ;}$ aS = array ('baaab', 'aaa', 'cc', 'bb', 'aaa', 'dd', 'aaaaa '); usort ($ aS, "cmpStr ");
This article is from Tao Shi's blog, please be sure to keep this source http://taoshi.blog.51cto.com/1724747/1302184