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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题