dtry54612 2014-05-30 19:59
浏览 112

如何快速查找已排序数组中的元素

I am new here but have checked previous posts about this and although similiar, not quite enough for what I'm trying to do. I have a CSV file with 40K+ records and retrieve LDAP records of 70K+ records; both stored in multidimensional array variable. The objective is to display all records that DO NOT match. My current solution is taking over 20 minutes to process which is inefficient. I created an outer loop that for each record it checks for a match in the LDAP recordset (inner loop), if found skip to next record and unset the LDAP array index to shrink the array for the next loop. I have also sorted both arrays in ascending order to speed up the process. Ideas, tweaks, help to speed up process?

foreach($csvArray as $csvindex=>$csvalue) { 
echo "<br />csvArray record: <strong> ".$counter."</strong><br />
";

  if($counter <= 1) {

      for ($i = 0, $max=$rs["count"]-1; $i < $max ;$i++) { //loop through ldap array
            if($csvalue[0] == $rs[$i]['uid'][0]) { // csv netid & ldap netid
                echo "CSV netid: ".$csvalue[0];
                echo "<br />matched LDAP array [$i] netid: ".$rs[$i]["uid"][0];
                echo "<br />
";
                $matched = $i; //$i represents integer offset in array (ie. $rs[21])
                break;
            }
      }

    } else {

    unset($rs[$matched]); //remove matched items
    $newRS = array_values($rs); //re-indexes array

    echo "Size of new LDAP array: ".count($newRS);

      for ($i=0, $max=count($newRS); $i<$max; $i++) {
          if($csvalue[0] == $newRS[$i]['uid'][0]) { // csv netid & ldap netid
            echo "<br />CSV netid: ".$csvalue[0];
            echo "<br />matched LDAP array [$i] netid: ".$newRS[$i]["uid"][0];
            echo "<br />
";
            $matched = $i; //$i represents integer offset in array (ie. $rs[21])
            break;
          }
      }

    } 

$counter++;
}

Example of what the original arrays look like (some info changed for security):

 //csvArray 
 Array (
 [0] => Array
    (
        [0] => ABABABAB
        [1] => test.account
        [2] => Chad
        [3] => Moeller
        [4] => chad.moeller@macmillan.com
        [5] => 9/10/2013 9:29 AM
    )

[1] => Array
    (
        [0] => D2L1.Test
        [1] => w40
        [2] => D2L 
        [3] => Test
        [4] => 
        [5] => 10/28/2013 4:24 PM
    )

//ldap multidimensional array
Array (
[count] => 67
[0] => Array
    (
        [uid] => Array
            (
                [count] => 1
                [0] => alackey1
            )

        [0] => uid
        [count] => 1
        [dn] => uid=alackey1,dc=edu
    )

[1] => Array
    (
        [uid] => Array
            (
                [count] => 1
                [0] => blamb3
            )

        [0] => uid
        [count] => 1
        [dn] => uid=blamb3,dc=edu
    )
  • 写回答

1条回答 默认 最新

  • duanjing4623 2014-05-30 20:27
    关注

    Here's a bit of code to replace your inner loop. It uses a binary search. The LDAP array must be sorted before this point.

    $workingArray=$newRS;
    while($LDAPcount=count($workingArray)) {
        $indexToCheck=ceil($LDAPcount/2);
        if($csvalue[0] == $workingArray[$indexToCheck]['uid'][0]) { // csv netid & ldap netid
            echo "<br />CSV netid: ".$csvalue[0];
            echo "<br />matched LDAP array ".$workingArray[$indexToCheck]["uid"][0];
            echo "<br />
    ";
            $matched = $indexToCheck; //$indexToCheck represents integer offset in array (ie. $rs[21])
            break;
        } else {
            if($csvalue[0] < $workingArray[$indexToCheck]['uid'][0]) {
                $workingArray=array_slice($workingArray,0,$indextoCheck);
            } else {
                $workingArray=array_slice($workingArray,$indextoCheck+1);
            }
        }
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。