PHP字符串比较

发布于 2024-12-18 22:43:17 字数 394 浏览 1 评论 0原文

我正在尝试比较网址的特定部分以获取结尾列表(它们是资源/“位置”,其中位置是状态缩写或字符串)。我用它来填充下拉菜单。这对于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷月断魂刀 2024-12-25 22:43:17

使用in_array() (http://php.net/manual/en/function.in-array.php)

$location = array();
$url = explode("/", $row['url']);
if(!in_array($url[2], $location))
{
    $location[$url[2]] = $url[2];
    echo '<option>'.$location[$url[2]].'</option>\n';
}

Use in_array() (http://php.net/manual/en/function.in-array.php)

$location = array();
$url = explode("/", $row['url']);
if(!in_array($url[2], $location))
{
    $location[$url[2]] = $url[2];
    echo '<option>'.$location[$url[2]].'</option>\n';
}
深居我梦 2024-12-25 22:43:17

你为什么还要使用 strcmp ?
另外,你为什么要重置$location?

$url = explode("/", $row['url']);
if(trim($location[$url[2]]) != trim($url[2]))
{
    echo '<option>'.$url[2].'</option>\n';
}

但不确定什么会导致重复,我想我需要一个例子来解决这个问题。

编辑:忽略上述内容,在阅读其他答案后,我看到您想要实现的目标。问题对此不太清楚。

why are you even using strcmp?
Also, why are you resetting $location?

$url = explode("/", $row['url']);
if(trim($location[$url[2]]) != trim($url[2]))
{
    echo '<option>'.$url[2].'</option>\n';
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文