dso15221 2013-09-10 16:28
浏览 23
已采纳

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

I have two arrays, one with a large set of URL paths and another with search IDs. Each of the URL paths has one unique ID in common. By the search ID we need to find the longest URL with the unique ID. Here is my code, I will explain a bit more later.

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

This gives the value of all matched IDs. Now if you see my array there are four sets of URL paths. What I want is that if I search with "961507" it should give:

 "D:\winwamp\www\info\961507\Good_Luck_Charlie\season_1\episode_3\The_Curious_Case_of_Mr._Dabney"

If i search with "961506", it should give:

"D:\winwamp\www\info\961506\Good_Luck_Charlie\season_1\episode_1\Study_Date" 

Now what I am getting are all the arrays that matched with my searched ID. Can you please help me to find out how can I accomplish this? Because I have more than 98000 URLs to sort out.

  • 写回答

1条回答 默认 最新

  • dongqi4085 2013-09-10 16:41
    关注

    Change the function as

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

    To print value use:

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

    Or

    $result = array();
    foreach($searchValues as $searchValue) {
    
      $result[] = searchstring($searchValue, $array);
    
    }
    
    print_r($result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?