RE: [PHP] What search algorithm does in_array() use?



Don't use a theta join - use a left, right or inner preferably with ANSI
syntax and make sure you indexes are set up correctly - this shouldn't
take very long to execute at all.

SELECT d.account
FROM data d
INNER JOIN accesslist a
USING account (or ON d.account=a.account)
WHERE a.username='username'

Respectfully,
Ligaya Turmelle
Systems Analyst
Guamcell Communications
Phone: (671)689-2377
-----Original Message-----
From: Ken Dozier [mailto:kdozier@xxxxxxxxxx]
Sent: Wednesday, January 31, 2007 6:31 AM
To: ceo@xxxxxxxxx
Cc: php-general@xxxxxxxxxxxxx; 'Gregory Beaver'
Subject: RE: [PHP] What search algorithm does in_array() use?

Thanks to all for your input, guys.

Regarding the construction of the SQL query, I would love to try it with
SUBSELECTs; but, alas, we are using RHEL 3 which ships with MySQL 3.23.
I don't think RH supports any 4.0 or later versions of MySQL on RHEL 3,
so I'm stuck with 3.23 for the time being. I had tried a query like
this:

SELECT d.account FROM data AS d, accesslist AS a WHERE
d.account=a.account AND a.username='username'

but it also took quite a bit of time. Since the problem would appear
for only a few users, I may have to simply hard code a workaround for
the time being and then come back to it later.

Thanks again for all of your help.

Ken.

-----Original Message-----
From: Richard Lynch [mailto:ceo@xxxxxxxxx]
Sent: Monday, January 29, 2007 6:13 PM
To: Ken Dozier
Cc: php-general@xxxxxxxxxxxxx
Subject: Re: [PHP] What search algorithm does in_array() use?

On Mon, January 29, 2007 11:20 am, Ken Dozier wrote:
Does in_array() use a search algorithm (i.e., binary search), or does
it check sequentially each element in the array?

Since there is no guarantee that the elements are in any particular
order, it almost has to be sequential...

I am using in_array() within a while{} loop to check query results
against an access-list array to produce a third array containing items

that successfully passed the comparison test. Because the two
starting arrays in the worst-case scenario can have 8,000 items each,
the loop is timing out.
Advice or alternative methods are appreciated.

Code Sample:
<?php
function check_results($results, $access_list) { # Check for $results
in array $access_list and
# add matches to array $match.

$result = false;
$match = array();

while ($r = mysql_fetch_row($results))
{ if ( in_array($r[0], $access_list) )
{ $match[] = $r; }
}

if ( count($match) > 0 ) { $result = $match; }

return $result;
}
?>

Ideal #1:
Figure out how to write an SQL query to find what's in the DB, and don't
do any search in PHP at all.

Ideal #2:
Put the values in as keys, and use http://php.net/isset This will "hash"
the values and have an O(1) lookup, I think.
Only works if you know the uniqueness of the values to be searched, or
don't care about duplicates.

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
http://www.php.net/unsub.php
.



Relevant Pages

  • Re: Need help on PHP for MPE/ix
    ... Here is the syntax of the dbupdate intrinsic in PHP: ... assoc array of item values) ... Please let me know if you have any more suggesstions to solve this problem. ... Pavan Kumar Rati wrote: ...
    (comp.sys.hp.mpe)
  • Re: Using a RegEx as a "variable" WITHIN an array?
    ... believe the PHP one is also based upon. ... combination of 1 or more numerics in this instance would give you your ... add this to your array - you could then do a pattern match on the array ... > Dim selCriteria as String ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Need help on PHP for MPE/ix
    ... If one item is an array, it must be declared as such in the list ... So, please show us the declaration of the record buffer, and its ... Of Pavan Kumar Rati ... Need help on PHP for MPE/ix ...
    (comp.sys.hp.mpe)
  • Re: attempting to return values from array from w/in a function
    ... What I get is an array w/ only the most recent array entry. ... I am new to PHP & do not have formal scripting ... you end up comparing strings, but the test you mean probably is simply: ... case 1: {codeblock} ...
    (comp.lang.php)
  • Re: Advice about fetching user information
    ... but actually *selecting* them in MySQL ... PHP scripts and use a gaugefunction to time pretty much every ... The same is pretty much true for searching an array in PHP - ... But I wouldn't be searching. ...
    (comp.lang.php)