Comparing arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
The Covenant
Forum Newbie
Posts: 11
Joined: Thu Sep 02, 2010 5:07 pm

Comparing arrays

Post 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:

Code: Select all

echo "You can create 'Leaf'";
... I've searched google up and down without luck :( can anyone help me with this?

Thanks in advance :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Comparing arrays

Post by Christopher »

Can you show us what your data looks like? And any code you have tried.
(#10850)
The Covenant
Forum Newbie
Posts: 11
Joined: Thu Sep 02, 2010 5:07 pm

Re: Comparing arrays

Post 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? :P I hope, cause I don't have any useful code to give you. :(

Thanks again for any help :)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Comparing arrays

Post 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   |
# +----------+
User avatar
egg82
Forum Contributor
Posts: 156
Joined: Sat Oct 01, 2011 9:29 pm
Location: Colorado, USA

Re: Comparing arrays

Post 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!
The Covenant
Forum Newbie
Posts: 11
Joined: Thu Sep 02, 2010 5:07 pm

Re: Comparing arrays

Post 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 :D
User avatar
egg82
Forum Contributor
Posts: 156
Joined: Sat Oct 01, 2011 9:29 pm
Location: Colorado, USA

Re: Comparing arrays

Post 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
Post Reply