2015-06-19 208 views
0

可以說我有串PHP比較字符串數組串

$string = "12315"; 

我想比較這陣列和想要的結果,因爲這:

$arr = [ 
    "123", //should be true 
    "115", //should be true 
    "111", //false 
    "132", //true 
    "1512" //true 
    "5531" //false 
] 

數組值不應該有每個計數數字大於給定的字符串 我應該怎麼做?先謝謝你!

+0

請解釋的' '12315''與'數組值' 123'',''115''比較如何應評估爲' true「,而與」111「的比較應該評估爲」false「。 – someOne

+0

@someOne因爲字符串「12315」包含兩個數字「1」,而「111」是三個數字「1」,所以它應該返回false –

+0

其相當明顯 – CodeGodie

回答

3

首先,你創建一個可能的組合,那麼你比較!

試試這個!

function create_possible_arrays(&$set, &$results) 
    { 
     for ($i = 0; $i < count($set); $i++) 
     { 
      $results[] = $set[$i]; 
      $tempset = $set; 
      array_splice($tempset, $i, 1); 
      $tempresults = array(); 
      create_possible_arrays($tempset, $tempresults); 
      foreach ($tempresults as $res) 
      { 
       $results[] = $set[$i] . $res; 
      } 
     } 
    } 
    $results = array(); 
    $str = '12315'; //your input string 
    $str = str_split($str); //create array 
    create_possible_arrays($str, $results); 
    $inputs = array(
     "123", //should be true 
     "115", //should be true 
     "111", //false 
     "132", //true 
     "1512", //true 
     "5531" 
    ); 
    foreach($inputs as $input){ 
     if(in_array($input,$results)){ 
      echo $input.' true <br/>'; 
     }else{ 
      echo $input.' false <br/>'; 
     } 
    } 

your result: 

    123 true 
    115 true 
    111 false 
    132 true 
    1512 true 
    5531 false 
+2

是的,Sathish,你是我的救星!簡單而優雅的答案!也謝謝其他人幫助我! :) –

+0

不客氣! – Sathish

0
foreach($array as $string) 
{ 
    if(strpos($searchstring, $string) !== false) 
    { 
    echo 'yes its in here'; 
    break; 
    } 
} 
+0

當我改變$ current_user_can_see = explode(「,」,「1,3,6,10,20,1,1」),它仍然顯示$ show爲「1,6,10」,我希望這不顯示(false) –

+0

你也可以用這種方式「$ match_index = array_search($ matches [0],$ array);」 –

+0

看到這個鏈接,你可能會得到主意http://stackoverflow.com/questions/10580547/php-compare-an-array-against-a-string-or-an-array-and-add-to-that-string –

0

使用array_map功能:

$string = '...'; 
$arr = ['..', '.', '..,', '....']; // true, true, false, false 
function check($str) { 
    /* Implement your check here */ 
    /* Smthng like: */ 
    $a = $string; 
    foreach ($i = 0; $i<strlen($str); $i++) { 
     $index = strpos($a, $str[$i]); 
     if ($index === false) return false; 
     else substr_replace($a,'', $index, 1); 
    } 
    return true; 
} 
$result = array_map(check, $arr); 
2
<?php 
$string = "12315"; 
$arr = ["123", "115", "111", "132", "1512", "5531"]; 

function specialCmp($str, $val) { 
    for($i=0; $i<strlen($val); $i++) { 
     $pos = strpos($str, $val[$i]); 
     if($pos !== false) 
      $str = substr_replace($str, '', $pos, 1); 
     else return false; 
    } 
    return true; 
} 

$out = array(); 
foreach($arr as $val) 
    $out[] = specialCmp($string, $val); 

echo '<pre>'; 
var_dump($out); 
echo '</pre>'; 
?> 
+1

你的回答也是正確的! StackOverflow可以讓我接受兩個答案嗎? –

0

試試這個:

$string = "123115"; 
$arr = [ 
    "123", 
    "115", 
    "111", 
    "132", 
    "1512", 
    "5531" 
]; 

$varr = array_filter($arr, function($el) use ($string) { 
     return (stristr($string,$el, true)!==false); 
    }); 
print_r($varr); 
+0

不正確,1512也應該返回(true) –