Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从给定的搜索ID中查找最长的数组值_Php_Arrays_String - Fatal编程技术网

Php 从给定的搜索ID中查找最长的数组值

Php 从给定的搜索ID中查找最长的数组值,php,arrays,string,Php,Arrays,String,我有两个数组,一个带有大量URL路径,另一个带有搜索ID。每个URL路径都有一个共同的唯一ID。根据搜索ID,我们需要找到具有唯一ID的最长URL。这是我的代码,稍后我将进一步解释 <?php function searchstring($search, $array) { foreach($array as $key => $value) { if (stristr($value, $search)) {

我有两个数组,一个带有大量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);
 }
?>
如果我用“961506”搜索,它应该给出:

 "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_Charlie\season_1\episode_1\Study_Date" 
现在我得到的是与我搜索的ID匹配的所有数组。你能帮我找出如何实现这一点吗?因为我有超过98000个URL需要整理。

将函数更改为

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;
}


“所有数组”或“所有数组项”?因为我只看到一个
$array
这是一个很好的使用地方,您可以在回拨中添加您的规则感谢我所希望的,但是return没有打印任何我必须回拨的内容,这是一个问题吗?
foreach($searchValues as $searchValue) {

  $result = searchstring($searchValue, $array);

  echo $result;

}
$result = array();
foreach($searchValues as $searchValue) {

  $result[] = searchstring($searchValue, $array);

}

print_r($result);