首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP -在两种情况下都在数组返回值中查找字符串中的数组单词

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

Stack Overflow用户
提问于 2018-06-12 14:49:24
回答 2查看 1.5K关注 0票数 1

问题

我有两个数组。数组包含单词,另一个数组包含字符串。我已经写了一个代码,可以在字符串中找到这个单词。如果在字符串中找到其中一个单词。我把话还给你。但是,如果在字符串中找到数组中的no,我也希望返回一个值。我写了几个代码,结果总是出现同样的情况。

这些案件是:

  • 当没有匹配的时候我没有价值
  • 我从之前的结果中得到了值。
  • 我得到了所有的价值匹配而没有匹配

问题

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

代码

输入

代码语言:javascript
复制
$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);

输出(我想以这样的方式结束

代码语言:javascript
复制
foreach ($CDNA_new as $string){
    $specie = $result ## Human
    echo $specie."-"$sring.  "<br \>\n"; 
}

在web浏览器中的结果:

人-人白细胞介素2 (IL2)a NA-氨苄西林耐药基因a 小鼠amp基因

初试

代码语言:javascript
复制
# 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

   } 

二次尝试

代码语言:javascript
复制
# 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
}

三次尝试

代码语言:javascript
复制
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
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-12 15:00:07

仅仅以你的第一个版本作为基础,就会出现一些问题。您没有重置用于存储匹配的字段,所以下一次它仍然有前一个循环中的匹配。

您还使用了$qspecie,并且设置了$specie

代码语言:javascript
复制
foreach($CDNA_new as $t){
    $match = null;    // Reset value for match
    # Go through the specie array
    foreach ($csv_specie as $c){
        # Find specie in string
        if (strpos($t, $c) !== FALSE ){
            $match = $c;
            break;       // Don't carry on if you found a match
        }
    }
    # If no match found set values to missing values
    if ($match == null){
        $match = "NA";
    }
    echo "----------------------".  "<br \>\n";
    echo '+'.$match.  "<br \>\n";
    echo '+'.$t.  "<br \>\n";
    # Work further with the values to retrieve gene ID using eSearch
}

或者您可以依赖于设置虚拟值,然后只有在找到真正匹配的情况下才会覆盖.

代码语言:javascript
复制
foreach($CDNA_new as $t){
    $match = "NA";
    # Go through the specie array
    foreach ($csv_specie as $c){
        # Find specie in string
        if (strpos($t, $c) !== FALSE ){
            $match = $c;
            break;
        }
    }
    echo "----------------------".  "<br \>\n";
    echo '+'.$match.  "<br \>\n";
    echo '+'.$t.  "<br \>\n";
    # Work further with the values to retrieve gene ID using eSearch
} 
票数 1
EN

Stack Overflow用户

发布于 2018-06-12 15:23:22

您可以使用preg_grep在物种循环中以不区分大小写的方式进行匹配。

然后使用array_diff从$cdna中删除项目,以确保不再匹配或浪费时间。

循环后的$cdna中剩下的是不匹配的项,我将它们添加到"N/A“项中。

代码语言:javascript
复制
$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"]);

输出:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50820053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档