1

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?

4

2 回答 2

2

使用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';
}
于 2011-11-30T14:55:48.350 回答
0

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

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

不知道是什么会导致重复,但我认为我需要一个例子来解决这个问题。

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

于 2011-11-30T15:01:09.923 回答