stripos 找不到值

正如您在输出中看到的,stripos 无法捕获 _formtoken 值,任何人都可以通过解决方案向我解释:


$value = ['_formtoken','expiry','version','pan','expiry','purchAmount','_formtoken','pan'];

for($i=0;$i<count($value);$i++){


  if (stripos($added, $value[$i]) == false) { 

      echo $value[$i] . ' => Not exists in <br>';    

      $added .= $value[$i];

  } 

  else { 

          echo $value[$i] . ' => already exists  <br>'; 

  } 


}

输出 :


_formtoken => Not exists in

expiry => Not exists in

version => Not exists in

pan => Not exists in

expiry => already exists

purchAmount => Not exists in

_formtoken => Not exists in

pan => already exists 



繁星淼淼
浏览 136回答 2
2回答

慕雪6442864

stripos 可能返回 0 或 false。如果您正在搜索的字符串位于搜索字符串的开头,它将返回 0。如果您在 if 语句中使用它,它将评估为 false。要解决此问题,请使用===.$value = ['_formtoken','expiry','version','pan','expiry','purchAmount','_formtoken','pan'];$added = null;for($i=0;$i<count($value);$i++){&nbsp; $pos = stripos($added, $value[$i]);&nbsp; if ( $pos === false) {&nbsp;&nbsp; &nbsp; &nbsp; echo $value[$i] . ' => Not exists in <br>' . "\n";&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; $added .= $value[$i];&nbsp; }&nbsp;&nbsp; else {&nbsp;&nbsp; &nbsp; &nbsp; echo $value[$i] . ' => already exists&nbsp; <br>'."\n";&nbsp;&nbsp; }&nbsp;}当我运行它时,这是我看到的输出。_formtoken => Not exists in <br>expiry => Not exists in <br>version => Not exists in <br>pan => Not exists in <br>expiry => already exists&nbsp; <br>purchAmount => Not exists in <br>_formtoken => already exists&nbsp; <br>pan => already exists&nbsp; <br>

杨__羊羊

看起来好像您正在尝试为数组中的每个唯一值创建一个字符串,对吗?如果是这样,试试这个,它更干净:$inputArray = ['_formtoken', 'expiry', 'version', 'pan', 'expiry', 'purchAmount', '_formtoken', 'pan'];// Only keep the first occurrence of each value$uniqueInputArray = array_unique($inputArray);// Output the values separated by a single space between eachecho implode(' ', $uniqueInputArray);或作为单行:echo implode(' ', array_unique(['_formtoken', 'expiry', 'version', 'pan', 'expiry', 'purchAmount', '_formtoken', 'pan']));
打开App,查看更多内容
随时随地看视频慕课网APP