PDA

View Full Version : Associative arrays


Jawelin
30 Jan 2002, 19:14
Hi. Where could I find an exaustive help with examples about multi-dimensional arrays ?
Actually I would - for a hack - store in memory some fields of a table of VB (user), to make some intensive operation without loading the db.
I thought a 2D array like this: $userArray[][]
Would use the two index, first as userid or other incremental value, second as the fields in the table.
For instance:

$userArray[][name]=$user[username]
$userArray[][mail]=$user[email]
... etc. in a cycle after a db fetch, but of course it doesn't work this way... ;)
Neither with an explicit first index as variable.

Do you have some example to load table in array with more values than the (key,value) as in php.net ?

Thanks a lot.
Bye

Mark Hensler
30 Jan 2002, 23:30
You do not want to do this. You will cram your RAM. (I made a rhym! ;))

It's better to build a query, then skip around the recordset using mysql_data_seek() (http://www.php.net/manual/en/function.mysql-data-seek.php).

Chen
31 Jan 2002, 09:54
Where's the ryhm?

Jawelin
31 Jan 2002, 10:17
... cram .... RAM ... :greedy:

So it would be more efficient go back & forth through the recordset instead of loading data into Ram ?
Isn't it too heavy to mySQL engine ?

I should compare new users data with old ones.
If I had 'N' news (let's say 10 at a time) and 'T' total old users (let's say +3000...), the complexity of the check should be:
N*T = 30.000 seeks on DB !!!!
:noid:

Any idea ?

Thanks.

P.S.: and what about using mysql_fetch_array() or mysql_fetch_row() just after the seek ? Either which ?

Mark Hensler
31 Jan 2002, 18:55
I'm not sure about mySQL load for mysql_fetch_array() vs mysql_fetch_row(), but the fetch_array will take up more memory (not much).

What is the hack for?
How often will this be run?

How much data are you pulling from the DB?
Do you need to pull all the fields, or just a few?
Can you throw any of the logic at the DB or does it need to be done in PHP?

Compare this...
30,000 DB seeks
... or ...
30,000 * (number of fields) demensional array

Chen
01 Feb 2002, 12:29
That is the weakest rhyme I've ever heard. ;)

Mark Hensler
01 Feb 2002, 15:36
*sigh* well, I tried :(

Jawelin
01 Feb 2002, 16:50
Originally posted by Mark Hensler
I'm not sure about mySQL load for mysql_fetch_array() vs mysql_fetch_row(), but the fetch_array will take up more memory (not much).

What is the hack for?
How often will this be run?
[...]

Well. I was trying to do myself an enhanced user moderation queue, mainly to findout potential dupe users when moderating new ones.
You can check here (http://www.vbulletin.org/forum/showthread.php?s=&threadid=33532) the original input, but would try to solve myself as far as i could.

By now I added a new action to admin/user.php like 'finddupe', called from the user moderation page.

Actually I would need to load in memory almost 3-4 columns of the user table as I should compare the single metaphone() (http://www.php.net/manual/en/function.metaphone.php) each others, measuring the levenshtein() (http://www.php.net/manual/en/function.levenshtein.php) distance between, for example, usernames/emails/ips/password-md5-hash (identical, of course), and so on.
This reason I though to memory as mySQL couldn't do anything itself.

Frequency: well, about once/twice a day, the times I moderate new users...
Do you think an array like this should overload the system ?

P.S.: anyway, any 2D arrays example ??? :supwink:

Thanks, poet!!! :china:

Mark Hensler
01 Feb 2002, 22:01
Why are your that paranoid? That seems like overkill. :rolleyes:

Anyway.. Your right, you couldn't do this in mySQL. I've never loaded an array of that size. I'm not sure what that would do to your system. :confused:

Some thoughts...
You don't have to calculate the metaphone() for each search. Instead, calculate it once for all existing users, and when new users register. Store the value in another field in the DB. Then you could use a simple query to test the metaphone() for new users.

The levenshtein is trickier, as you can't preproccess anything and store it in the DB.

What did you want to do to the IP? levenshtein()? You may be able to do something similar by using wildcards in mySQL. For example, most dialups will only vary in the last 2 octets.


Just some thoughts.

Jawelin
01 Feb 2002, 22:13
They are all parameters that - took together - can build a complex profile about a dupe user.

Paranoid? Yeah! I saw for months people subscribing again and again just because they wouldn't remember the password...
I explained well talking about the hack I linked to above.
Thanks, hwr.