我有两个数组,一个有大量的URL路径,另一个有搜索ID。每个URL路径都有一个共同的唯一ID。通过搜索ID,我们需要找到具有唯一ID的最长URL。这是我的代码,我稍后会解释一下。
<?php
function searchstring($search, $array) {
foreach($array as $key => $value) {
if (stristr($value, $search)) {
echo $value;
}
}
return false;
}
$array = array(
"D:\winwamp\www\info\961507\Good_Luck_Charlie",
"D:\winwamp\www\info\961507\Good_Luck_Charlie\season_1",
"D:\winwamp\www\info\961507\Good_Luck_Charlie\season_1\episode_3",
"D:\winwamp\www\info\961507\Good_Luck_Charlie\season_1\episode_3\The_Curious_Case_of_Mr._Dabney",
"D:\winwamp\www\info\961506\Good_Luck_Charl",
"D:\winwamp\www\info\961506\Good_Luck_Charlie\season_1",
"D:\winwamp\www\info\961506\Good_Luck_Charlie\season_1\episode_1",
"D:\winwamp\www\info\961506\Good_Luck_Charlie\season_1\episode_1\Study_Date");
$searchValues = array("961507","961506");
foreach($searchValues as $searchValue) {
$result = searchstring($searchValue, $array);
}
?>
这给出了所有匹配ID的值。现在,如果您看到我的数组,则有四组URL路径。我想要的是,如果我用“961507”搜索,它应该给出:
"D:\winwamp\www\info\961507\Good_Luck_Charlie\season_1\episode_3\The_Curious_Case_of_Mr._Dabney"
如果我用“961506”搜索,它应该给出:
"D:\winwamp\www\info\961506\Good_Luck_Charlie\season_1\episode_1\Study_Date"
现在我得到的是与我搜索到的ID匹配的所有数组。你能帮我找一下如何实现这个目标吗?因为我有超过98000个网址需要整理。
答案 0 :(得分:1)
将功能更改为
function searchstring($search, $array) {
$length = 0;
$result = "";
foreach($array as $key => $value) {
if (stristr($value, $search)) {
if($length < strlen($value)) {
$length = strlen($value);
$result = $value;
}
}
}
return $result;
}
打印价值使用:
foreach($searchValues as $searchValue) {
$result = searchstring($searchValue, $array);
echo $result;
}
或
$result = array();
foreach($searchValues as $searchValue) {
$result[] = searchstring($searchValue, $array);
}
print_r($result);