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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem merging arrays. Help!

Status
Not open for further replies.

JediDan

Programmer
May 15, 2002
128
US
I am trying to merge to arrays that are results of SQL queries. Let's say we have these two arrays:

Code:
array 1
=======
DAN
JOHN
SUE
MIKE
JIM
FRANK

array 2
=======
DAN
JOHN
SUE
MIKE
JIM
ARNOLD

The result, using either array_merge() or the
Code:
+
operator is:

Code:
DAN
JOHN
SUE
MIKE
JIM
ARNOLD

...which is simply the second array. I want the result to be:

Code:
DAN
JOHN
SUE
MIKE
JIM
FRANK
ARNOLD

Order is not particularly important. What can I do??

Any help is appreciated.
 
Can you show us the line of code you're using? array_merge() should do what you want. --
How can you be in two places at once when you're not anywhere at all?
 
This is the part in question:

Code:
$lrows = OCIFetchStatement($query1SQL,$liveres);
$brows = OCIFetchStatement($query2SQL,$baseres);
	
$both = array_merge($liveres,$baseres);

print '<BR><TABLE border=1 width=100%>';
for ($i = 0; $i <= max($lrows,$brows); $i++)
{
     $col = $both['RESULT'];
     $data = $col[$i];
		
     print '<TR>';
     print '<TD>'.$data.'</TD>';
     print '</TR>';
}
print '</TABLE>';

For a variety of reasons, I can't simply use the SQL INTERSECT, UNION or MINUS operators. This has to be done at the page level.

Thanks for your help.
 
Something fishy is going on...

<?
$array1 = array(&quot;one&quot;, &quot;two&quot;, &quot;three&quot;);
$array2 = array(&quot;one&quot;, &quot;two&quot;, &quot;four&quot;);
$merged1 = array_merge($array1, $array2);
$merged2 = $array1 + $array2;

echo &quot;array_merge<br>&quot;;
for ($i = 0; $i <= 6; $i++)
{
echo $merged1[$i].&quot; &quot;;
}
echo &quot;<br><br>&quot;;

echo &quot;+<br>&quot;;
for ($i = 0; $i <= 6; $i++)
{
echo $merged2[$i].&quot; &quot;;
}
?>

For me, the result of the code above is:

---
array_merge
one two three one two four

+
one two three
---

The array_merge is doing exactly what I would expect. It appended the second onto the first and didn't filter any duplicates. But that plus thing I don't quite understand. It just gave me the first array, and elements 3-5 are empty.

Anyhow, for the results you want, it looks like you're going to have to do it the hard way. Create a third array with all the elements of array one. Then, for each item in array two, check to see if it exists in the third array. If not, add it. If so, skip it.
 
I don't know the Oracle functions, but I assume that OCIFetchStatement($a,$b) puts the results of query $a into array $b?

You might print_r() each array to confirm that they contain what you expect.

I'm a bit confused by your for statement, which probably stems from my lack of understanding of the OCIFetchStatement() function.

It seems like you're setting $col to the value of $both represented by the key &quot;RESULT&quot;, then getting the $i'th value of that. Wouldn't it make more sense to use foreach() or at least to be looking at the values in $both rather than a derivative of it?

Again, I don't know what is returned by the OCI* commands, so I don't know if you're getting an assoc array, an array of arrays, or what have you. So a print_r of the three arrays ($both, $liveres, $baseres) would help. --
How can you be in two places at once when you're not anywhere at all?
 
OCIFetchStatement($a,$b) returns all of the rows for a query $a (so you can get a count), and puts them in $b.

The idea is that this is the only reasonable way to get a count of how many records are returned by a query. (Plus I want them in an array anyhow).

I have printed out both of the arrays before they get combined into $both, and the results are correct.

print_r($both) showed that $both only really contains values of the second array.

I suppose foreach() could be used, but I think that returns a copy as well (not a reference), and my code was the only way I could iteratively get all the values of one column, since array[j] type syntax doesn't really seem to work in php.

But I guess I'll have to do as disord3r says, and use a third array or re-engineer the whole thing...

Thank all for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top