php - How to quickly find elements in a sorted array -
i new here have checked previous posts , although similiar, not quite enough i'm trying do. have csv file 40k+ records , retrieve ldap records of 70k+ records; both stored in multidimensional array variable. objective display records not match. current solution taking on 20 minutes process inefficient. created outer loop each record checks match in ldap recordset (inner loop), if found skip next record , unset ldap array index shrink array next loop. have sorted both arrays in ascending order speed process. ideas, tweaks, speed process?
foreach($csvarray $csvindex=>$csvalue) { echo "<br />csvarray record: <strong> ".$counter."</strong><br />\n"; if($counter <= 1) { ($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 />\n"; $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); ($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 />\n"; $matched = $i; //$i represents integer offset in array (ie. $rs[21]) break; } } } $counter++; }
example of original arrays (some info changed 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 ) [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 )
here's bit of code replace inner loop. uses binary search. ldap array must sorted before 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 />\n"; $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); } } }
Comments
Post a Comment