Page 1 of 1
Comparing arrays
Posted: Wed Sep 28, 2011 5:10 am
by The Covenant
Hi. I've been having serious problems with a script I'm trying to write... Hoping you can help
If you've ever played Diablo II, you're gonna know what I'm talking about. In the game you can collect runes that you can put into socketed items, combining them, creating a runeword that gives the item some cool attributes. So. I'm programming a website where I can add all the runes I've collected in the game and put them into a database. That part I've got covered. Next I want to add specific runewords in another database and write a script that dynamicaly checks if I've collected enough runes to form a runeword and if so notify me with a simple:
... I've searched google up and down without luck

can anyone help me with this?
Thanks in advance

Re: Comparing arrays
Posted: Wed Sep 28, 2011 10:43 am
by Christopher
Can you show us what your data looks like? And any code you have tried.
Re: Comparing arrays
Posted: Thu Sep 29, 2011 4:31 am
by The Covenant
My database containing runes looks like this:
ID = 0
NAME = Ral
Amount = 3
and so on... I get this data using mysql_fetch_array / mysql_fetch_assoc... And this is how far I've come, because I don't know how I should store the runewords in a database so that I later on can compare them to single runes. Example:
One runeword is made by putting together the runes: "Tal" and "Eth". So, if I have those two runes in the other database, I want to echo out something that would notify me of the available runeword.
Is this making any sense?

I hope, cause I don't have any useful code to give you.
Thanks again for any help

Re: Comparing arrays
Posted: Sat Oct 01, 2011 11:21 am
by McInfo
Code: Select all
DESCRIBE rune;
# +----------+-------------+------+-----+---------+----------------+
# | Field | Type | Null | Key | Default | Extra |
# +----------+-------------+------+-----+---------+----------------+
# | id | int(3) | NO | PRI | | auto_increment |
# | name | varchar(10) | NO | | | |
# | quantity | int(3) | NO | | 0 | |
# +----------+-------------+------+-----+---------+----------------+
DESCRIBE word;
# +-------+-------------+------+-----+---------+----------------+
# | Field | Type | Null | Key | Default | Extra |
# +-------+-------------+------+-----+---------+----------------+
# | id | int(3) | NO | PRI | | auto_increment |
# | name | varchar(30) | NO | | | |
# +-------+-------------+------+-----+---------+----------------+
DESCRIBE word_rune;
# +---------+--------+------+-----+---------+-------+
# | Field | Type | Null | Key | Default | Extra |
# +---------+--------+------+-----+---------+-------+
# | word_id | int(3) | NO | PRI | | |
# | socket | int(2) | NO | PRI | | |
# | rune_id | int(3) | NO | | | |
# +---------+--------+------+-----+---------+-------+
SELECT * FROM rune;
# +----+------+----------+
# | id | name | quantity |
# +----+------+----------+
# | 1 | El | 4 |
# | 2 | Eld | 0 |
# | 3 | Tir | 0 |
# | 4 | Nef | 0 |
# | 5 | Eth | 2 |
# | 6 | Ith | 0 |
# | 7 | Tal | 1 |
# | 8 | Ral | 1 |
# | 9 | Ort | 0 |
# +----+------+----------+
SELECT * FROM word;
# +----+------------------+
# | id | name |
# +----+------------------+
# | 1 | Ancient's Pledge |
# | 2 | Beauty |
# +----+------------------+
SELECT * FROM word_rune;
# +---------+--------+---------+
# | word_id | socket | rune_id |
# +---------+--------+---------+
# | 1 | 1 | 8 |
# | 1 | 2 | 9 |
# | 1 | 3 | 7 |
# | 2 | 1 | 1 |
# | 2 | 2 | 8 |
# | 2 | 3 | 5 |
# +---------+--------+---------+
SELECT word.name AS word, rune.name AS rune, quantity
FROM word_rune
JOIN word ON word_id = word.id
JOIN rune ON rune_id = rune.id;
# +------------------+------+----------+
# | word | rune | quantity |
# +------------------+------+----------+
# | Ancient's Pledge | Ral | 1 |
# | Ancient's Pledge | Ort | 0 |
# | Ancient's Pledge | Tal | 1 |
# | Beauty | El | 4 |
# | Beauty | Ral | 1 |
# | Beauty | Eth | 2 |
# +------------------+------+----------+
SELECT word.name AS eligible
FROM word_rune
JOIN word ON word_id = word.id
JOIN rune ON rune_id = rune.id
GROUP BY word_id
HAVING MIN(quantity) > 0;
# +----------+
# | eligible |
# +----------+
# | Beauty |
# +----------+
Re: Comparing arrays
Posted: Sat Oct 01, 2011 11:27 pm
by egg82
for a quick array comparison, just use something like:
Code: Select all
$end = count($array1);
for($i=1;$i<$end;$i++){
if($array1[$i] == $array2[$i]){
// code here
}else{
// code here
}
}
(Of course, this does assume that $array1 and $array2 are both the same legth)
Otherwise, to be honest I have no idea what you're trying to do... Sorry!
Re: Comparing arrays
Posted: Sun Oct 02, 2011 9:34 am
by The Covenant
Awesome

Thank you so much for your help

I'll get to work deciphering this and see if I can make sense of it

Re: Comparing arrays
Posted: Sun Oct 02, 2011 11:26 am
by egg82
Glad I could help ^.^
Like I said, however. That assumes array1 and array2 are the same exact length. If they aren't, you'll get an error