Chippewa Clare Clinton Crawford Delta Dickinson Eaton Emmet 为什么跳过Baraga?我选择哪个组无关紧要。它总是跳过foreach代码中的第一个。我试过了: strpos($ saved_counties,$ one_county ['County'])> = 0 那个结果相同。 我也试过: strpos($ saved_counties,$ one_county ['County' ])> 0 最终重新选择了所有这些。 $县是一个存储所有83密歇根州的阵列县。 $ saved_counties只是一个逗号分隔的字符串,用于存储用户以前的选择。解决方案 saved_counties; echo'< br />< br />所有县的名单:'; foreach( 县作为 one_county): echo'< br />'. This is making my eyes go nuts right now. It should work but it's not. I'm storing the values (county names) the user chose from a multiselect select in a comma delimited string. Then when the form is submitted and rejected I'm using that string to "select" the ones that were previously selected.What I have tried:Here's my test code:<?php echo 'Previous selection: '.$saved_counties; echo '<br /><br />List of all counties:'; foreach ($counties as $one_county): echo '<br />'.$one_county['County']; if (strpos($saved_counties, $one_county['County'])) { echo ' (**** IN PREVOUS SELECTION ***)'; } endforeach;?>When I run the code, this is what I get:Previous selection: Baraga,Barry,BayList of all counties:AlconaAlgerAlleganAlpenaAntrimArenacBaragaBarry (**** IN PREVOUS SELECTION ***)Bay (**** IN PREVOUS SELECTION ***)BenzieBerrienBranchCalhounCassCharlevoixCheboyganChippewaClareClintonCrawfordDeltaDickinsonEatonEmmetWhy is it skipping "Baraga"? It doesn't matter which group I select. It always skips the first one in the "foreach" code. I tried:strpos($saved_counties, $one_county['County']) >= 0That rendered the same results.I also tried:strpos($saved_counties, $one_county['County']) > 0That ended up reselecting ALL of them.$counties is an array that stores ALL 83 michigan counties. $saved_counties is just a comma delimited string that stores the user's previous selection. 解决方案 saved_counties; echo '<br /><br />List of all counties:'; foreach (counties asone_county): echo '<br />'. 这篇关于Php strpos没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!