php - How can I efficiently search a field for flipped word order? -


i have following array.

$arr = array('foo','bar','foo-bar','abc','def','abc-def','ghi','abc-def-ghi'); 

i'm given new string decide add array or not. if string in array, don't add it. if not in array in current form, in flipped word form found, don't add it.

how should accomplish this?

examples:

'foo'     —->  n  - not add, found 'xyz'     —->  y  - add, new 'bar-foo' —->  n  - not add, found in flipped form 'foo-bar' 'ghi-jkl' —->  y  - add, new 

what recommend?

if want exclude items elements ('abc','ghi', etc.) contained in order , not reversed, do:

$arr = array('foo','bar','foo-bar','abc','def','abc-def','ghi','abc-def-ghi');  function split_and_sort($str) {     $partsa = explode('-', $str);     sort($partsa);     return $partsa; } $arr_parts = array_map('split_and_sort', $arr);  $tests = array('foo','xyz','bar-foo','ghi-jkl'); $tests_parts = array_map('split_and_sort', $tests);  foreach($tests_parts $test) {     if( !in_array($test, $arr_parts)) {         echo "adding: " . join('-', $test) . "\n";         $arr[] = join('-', $test);     }     else {         echo "skipping: " . join('-', $test) . "\n";     } } var_export($arr); 

which outputs:

skipping: foo adding: xyz skipping: bar-foo adding: ghi-jkl array (   0 => 'foo',   1 => 'bar',   2 => 'foo-bar',   3 => 'abc',   4 => 'def',   5 => 'abc-def',   6 => 'ghi',   7 => 'abc-def-ghi',   8 => 'xyz',   9 => 'ghi-jkl', ) 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -