Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Merging Arrays

Status
Not open for further replies.

OrangeWire

IS-IT--Management
Mar 26, 2003
28
US
After reading php.net all day Im REALLY confused about how i should go about doing this...


i would like to merge 3 arrays into a single that
1) Does not have duplicate entries
2) Keeps the order of elements

Here is the code i have so far:

//Dont Mess With This Stuff, Jigga
mysql_connect($host, $user, $password) or die("Could not connect: " . mysql_error());
mysql_select_db($db);


$closest = 'SELECT `id_gal` '
. ' FROM `gals` '
. ' WHERE `gallery_text` '
. ' LIKE \'%pic%\' AND `main_page_link` '
. ' LIKE \'%pic%\' AND `main_page_description` '
. ' LIKE \'%pic%\'';

$closer = 'SELECT `id_gal` '
. ' FROM `gals` '
. ' WHERE `gallery_text` '
. ' LIKE \'%pic%\' AND `main_page_description` '
. ' LIKE \'%pic%\'';

$close = 'SELECT `id_gal` '
. ' FROM `gals` '
. ' WHERE `main_page_description` '
. ' LIKE \'%pic%\'';

$result1 = mysql_query($closest);
$result2 = mysql_query($closer);
$result3 = mysql_query($close);

print "<h1>CLOSEST:</h1><br>";
while ($row = mysql_fetch_array($result1)){
print $row[0] . "<br>\n";
}

print "<br><br><h1>CLOSER:</h1><br>";
while ($row = mysql_fetch_array($result2)){
print $row[0] . "\t" . $row[1] . "<br>\n";
}

print "<br><br><h1>CLOSE:</h1><br>";
while ($row = mysql_fetch_array($result3)){
print $row[0] . "\t" . $row[1] . "<br>\n";
}



The print statements are there to prove to me that i get the correct output:

CLOSEST:

14
62
74
82
90
93
96
100
101
149
211
314
319
374
388
390
391
407
421
570
577



CLOSER:

2
13
14
17
20
21
28
31
37
42
48
49
50
53
62
63
64
65
....(100 + more results)



CLOSE:

2
5
13
14
17
20
21
27
28
31
37
38
42
48
49
50
52
53
62
63
64
65
....(300 + more results)



Thanks very much for your time!!
 
i would like to merge these 3 arrays into a single that
1) Does not have duplicate entries
2) Keeps the order of elements


$result1 = mysql_query($closest);
$result2 = mysql_query($closer);
$result3 = mysql_query($close);



im a newbie so i might be using the wrong terminology without knowing it.
 
I don't know why it is you're trying to run three queries and merge. Once you've done all that, you'll find that you answer will always be the set of values you've called "CLOSE".


You're running three queries:
[tt]SELECT
id_gal
FROM
gals
WHERE
main_page_description LIKE '%pic%'


SELECT
id_gal
FROM
gals
WHERE
gallery_text LIKE '%pic%' AND
main_page_description LIKE '%pic%'


SELECT
id_gal
FROM
gals
WHERE
gallery_text LIKE '%pic%' AND
main_page_link LIKE '%pic%' AND
main_page_description LIKE '%pic%'
[/tt]
(These are in reverse order from the order in which you initialize the query strings in your script.)

The second query is simply the first query with an additional WHERE clause ANDed to it.

The third query is simply the second query with an additional WHERE clause ANDed to it.


With those ANDs between the queries, the least restrictive query (the first in my ordering) will always include all rows that would be returned by the other two. Just use the least restrictive query by itself.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
what i am doing is trying to write a simple search engine that parses some data i have stored in 3 areas.

If it finds the word in all three areas (As specified) the result is the Most relevant

if it finds it in only 2 areas(As specified), those results are more relevant.

if it finds results in only 1 area (As specified)the returned data is the least relevant.

I would then like to combine ALL elements that are returned into a single array that i can then use to list all search results.

The value of the combined array should be all unique values from all three arrays stacked in the order they were processed.


If this makes no sense its because im a total newbie. thanks for helping me guys!
 
In that case, create a single empty array.

Run through all three while() loops you already have, instead of printing out the rows append them to the array.

Use either array_key_exists() or isset() to determine whether a row has already been appended to the array before performing the append.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
how can i use those functions ?
do i put them in a loop?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top