PHP - 在两种情况下,在数组返回值中查找字符串中的数组

时间:2018-06-12 14:49:25

标签: php arrays matching

问题

我有两个数组。 1个数组包含单词,另一个数组包含字符串。我编写了一个代码,可以在字符串中找到该单词。 如果在字符串中找到其中一个单词。我回复了这个词。但是如果在字符串中找不到数组中的单词,我也想返回一个值。我写了几个代码,最后总是有相同的情况。

案例是:

  • 没有比赛时没有价值
  • 我从
  • 之前的结果中得到了值
  • 我得到了每一个值匹配而且没有回匹配

问题

我错了。如果找到该单词,如何为每个字符串存储该单词,如果未找到,则将该值设置为缺失值?

代码

输入
$csv_specie = array("Mouse","Human");
$CDNA = 'Human interleukin 2 (IL2)a;Ampicillin resistance gene (amp)a;Mouse amp gene';
# Split string by ; symbol into an array
  $CDNA_new = preg_split("/\b;\b/", $CDNA);
输出(我想结束这样的事情
foreach ($CDNA_new as $string){
    $specie = $result ## Human
    echo $specie."-"$sring.  "<br \>\n"; 
}

Web浏览器中的结果:

  

人 - 人白细胞介素2(IL2)a

     

NA-氨苄青霉素抗性基因(amp)a

     

小鼠 - 小鼠amp基因

首先尝试

# Go through the string
  foreach($CDNA_new as $t){
# Go through the specie array
    foreach ($csv_specie as $c){ 
# Find specie in string
        if (strpos($t, $c) !== FALSE ){ 
            $match = $c;
            $specie = $c;
        }
    }
# If no match found set values to missing values
    if (isset($specie) !== TRUE){
        $match = "NA";
        $specie = "NA";
        }
    echo "----------------------".  "<br \>\n"; 
    echo '+'.$specie.  "<br \>\n"; 
    echo '+'.$match.  "<br \>\n"; 
    echo '+'.$t.  "<br \>\n";
    # Work further with the values to retrieve gene ID using eSearch

   } 

第二次尝试

# use function to find match
function existor_not($str, $character) {
    if (strpos($str, $character) !== false) {
        return $character;
    }
    return $character = "0";
}
foreach ( $CDNA_new as $string ){

    foreach ( $csv_specie as $keyword ){

        $test = existor_not($string,$keyword);
    }
    echo "-".$test."|" . $string.  "<br \>\n"; 
    # Work further with the values to retrieve gene ID using eSearch
}

第三次尝试

foreach ( $CDNA_new as $string ){
  foreach ( $csv_specie as $keyword ){
    $result = stripos($string, $keyword);
    if ($result === false) {
        $specie = "NA";
    }
    else {
        $specie = $keyword;
    }
}
if ($specie !== "NA"){
echo "match found";
}else{
   $match = "NA";
   $specie = "NA";
}
    echo $specie. "<br \>\n"; 
    # Work further with the values to retrieve gene ID using eSearch
    }

2 个答案:

答案 0 :(得分:1)

仅使用您的第一个版本作为基础,存在一些问题。你没有重置你用来存储比赛的字段,所以下一次它仍然有来自前一个循环的匹配。

您还在使用RewriteRule并且设置了500

$qspecie

或者您可以依靠设置虚拟值,然后只有在找到真正匹配时才会覆盖...

$specie

答案 1 :(得分:1)

你可以使用preg_grep以不区分大小写的方式匹配一个实体循环。
然后我使用array_diff从$ cdna中删除项目以确保我不再匹配或浪费时间 在循环之后$ cdna中剩下的是不匹配的项目,我将它们添加到“N / A”项目。

$csv_specie = array("Mouse","Human");
$CDNA = 'Human interleukin 2 (IL2)a;Ampicillin resistance gene (amp)a;Mouse amp gene;Some other stuff unknown to man kind';


$csv_specie = array("Mouse","Human");
$CDNA = 'Human interleukin 2 (IL2)a;Ampicillin resistance gene (amp)a;Mouse amp gene;Some other stuff unknown to man kind;some other human stuff';

$cdna = explode(";", $CDNA);

Foreach($csv_specie as $specie){
    $matches[$specie] = preg_grep("/\b" . $specie . "\b/i", $cdna);
    Echo $specie . " - " . implode("\n" . $specie . " - " , $matches[$specie]) . "\n";

    // Remove matched items from $cdna
    // This makes $cdna smaller for each 
    // iteration and make it faster.
    $cdna = array_diff($cdna, $matches[$specie]);
}

// What is left in $cdna is not matched
$matches["N/A"] = $cdna;

Echo "\nN/A - " . implode("\nN/A - ", $matches["N/A"]);

输出:

Mouse - Mouse amp gene
Human - Human interleukin 2 (IL2)a
Human - some other human stuff

N/A - Ampicillin resistance gene (amp)a
N/A - Some other stuff unknown to man kind

https://3v4l.org/64Qmq