PHP字符串比较
我正在尝试比较网址的特定部分以获取结尾列表(它们是资源/“位置”,其中位置是状态缩写或字符串)。我用它来填充下拉菜单。这对于 2 个字母的状态效果很好,但是当我比较字符串时,它仍然显示重复项。这是我正在使用的代码,“National”是不会被过滤掉的重复字符串。
$url = explode("/", $row['url']);
if(strcmp(trim($location[$url[2]]),trim($url[2])) != 0)
{
$location = array($url[2] => $url[2]);
echo '<option>'.$location[$url[2]].'</option>\n';
}
有没有更好的方法来比较字符串?
I'm trying to compare a specific part of a url to get a list of the endings (which are resource/'location' where location is either state initials or a string). I'm using this to populate a drop down menu. This works well with the 2 letter states, but when I compare the strings it still shows duplicates. This is the code I am working with, and 'National' is the repeated string that does not get filtered out.
$url = explode("/", $row['url']);
if(strcmp(trim($location[$url[2]]),trim($url[2])) != 0)
{
$location = array($url[2] => $url[2]);
echo '<option>'.$location[$url[2]].'</option>\n';
}
Is there a better way to compare strings?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
in_array()
(http://php.net/manual/en/function.in-array.php)Use
in_array()
(http://php.net/manual/en/function.in-array.php)你为什么还要使用 strcmp ?
另外,你为什么要重置$location?
但不确定什么会导致重复,我想我需要一个例子来解决这个问题。
编辑:忽略上述内容,在阅读其他答案后,我看到您想要实现的目标。问题对此不太清楚。
why are you even using strcmp?
Also, why are you resetting $location?
Not sure what would cause the duplicates though, think I'd need to have an example to play with to figure that out.
EDIT: Ignore the above, after reading the other answer I see what you're trying to achieve. The question wasn't very clear on that.