问题
我有两个数组。数组包含单词,另一个数组包含字符串。我已经写了一个代码,可以在字符串中找到这个单词。如果在字符串中找到其中一个单词。我把话还给你。但是,如果在字符串中找到数组中的no,我也希望返回一个值。我写了几个代码,结果总是出现同样的情况。
这些案件是:
问题
我哪里搞错了。如果找到,如何为每个字符串存储单词,如果未找到,则将值设置为缺失值。
代码
输入
$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-氨苄西林耐药基因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
}发布于 2018-06-12 15:00:07
仅仅以你的第一个版本作为基础,就会出现一些问题。您没有重置用于存储匹配的字段,所以下一次它仍然有前一个循环中的匹配。
您还使用了$qspecie,并且设置了$specie。
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
}或者您可以依赖于设置虚拟值,然后只有在找到真正匹配的情况下才会覆盖.
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
} 发布于 2018-06-12 15:23:22
您可以使用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 kindhttps://stackoverflow.com/questions/50820053
复制相似问题