2013-07-20 248 views
0

基本上,我有一個字符串數組,我想檢查每個字符串中的每個字符是否在預定義的$source字符串中。這裏是如何,我認爲它應該做的事:如何遍歷字符串的字符?

$source = "abcdef"; 
foreach($array as $key => $value) { 
    foreach(char in $value) { 
     if(char is not in source) 
      unset($array[$key]); //remove the value from array 
    } 
} 

如果這是一個正確的邏輯,如何實現foreachif部分?

+1

嘗試爆炸的$源字符數組,然後在你的循環中使用[array_intersect()](http://www.php.net/manual/en/function.array-intersect.php) –

+0

因此,基本上我應該調用'$ s ource_array = explode(「」,$ source);'併爲'$ value'做同樣的事情?空'分隔符'返回False。它應該是什麼? – iTurki

+1

使用[str_split()](http://www.php.net/manual/en/function.str-split.php) –

回答

0

試試這個代碼:

$source = "abcdef"; 
foreach($array as $key => $value) { 
    $ichr = strlen($value) - 1; 
    // traverses each character in string 
    for($i=0; $i<$ichr; $i++) { 
    if(stristr($value{$i}) === false) { 
     unset($array[$key]); 
     break; 
     } 
    } 
} 
1

你可以試試這個:

$array = array('1' => 'cab', '2' => 'bad', '3' => 'zoo'); 
$source = "abcdef"; 
foreach($array as $key => $value) { 
    $split = str_split($value); 
    foreach($split as $char){ 
     $pos = strrpos($source, $char); 
     if ($pos === false) { 
      unset($array[$key]); 
      break; 
     } 
    } 
} 

結果:

array(2) { 
    [1]=> 
    string(3) "cab" 
    [2]=> 
    string(3) "bad" 
} 

DEMO:http://codepad.org/fU99Gdtd

+0

太好了。任何想法如何重新索引數組後,你取消了它的一些元素? – iTurki

+0

@iturki試試這個:http://codepad.org/3mouGpFG –

0
$array = array("abc","defg","asd","ade","de","fe"); 
$source = "abcde"; 
foreach ($array as $key => $string){ 
    for($i=0;$i<strlen($string);$i++){ 
    if(strpos($source, $string[$i])===false){ 
     unset($array[$key]); 
    } 
    } 
} 

現在陣列看起來像

array(3) { 
    [0]=> 
    string(3) "abc" 
    [3]=> 
    string(3) "ade" 
    [4]=> 
    string(2) "de" 
} 
0

據我瞭解,你要過濾(刪除)字符,未在$源變量定義。馬克·貝克的評論,這是你所需要的:

$source = str_split ("abdef"); //defined characters 
$target = str_split ("atyutyu"); //string to be filtered 

$result = array_intersect ($target, $source); 
echo implode($result); // output will be only "a" 

而完整的例子:

$source = str_split ("abdef"); 
$txts = array ("alfa", "bravo", "charlie", "delta"); 

function filter ($toBeChecked, $against) 
{ 
    $target = str_split ($toBeChecked); 
    return implode (array_intersect ($target, $against)); 
} 
foreach ($txts as &$value) 
{ 
    $value = filter ($value, $source); 
} 
foreach ($txts as $value) 
{ 
    echo $value . ", "; 
} 
//output afa, ba, ae, ae 
0
$array = array(
    'abacab', 
    'baccarat', 
    'bejazzle', 
    'barcode', 
    'zyx', 
); 


$source = "abcde"; 
$sourceArray = str_split($source); 

foreach($array as $value) { 
    $matches = array_intersect($sourceArray, str_split($value)); 
    echo $value; 
    if (count($matches) == 0) { 
     echo ' contains none of the characters ', $source, PHP_EOL; 
    } elseif (count($matches) == count($sourceArray)) { 
     echo ' contains all of the characters ', $source, PHP_EOL; 
    } else { 
     echo ' contains ', count($matches), ' of the characters ', $source, ' (', implode($matches), ')', PHP_EOL; 
    } 
} 

abacab contains 3 of the characters abcde (abc) 
baccarat contains 3 of the characters abcde (abc) 
bejazzle contains 3 of the characters abcde (abe) 
barcode contains all of the characters abcde 
zyx contains none of the characters abcde