Forum Moderators: coopster

Message Too Old, No Replies

PHP array problem

php,array,comparing,comparing arrays

         

webdesigner

2:31 am on Nov 16, 2007 (gmt 0)

10+ Year Member



Hi, I have read posts here and have found many answers to my own questions but this time I thought I would post a question as i can't find an answer .

I am trying to take a group of arrays (about 10) and find out which values in those arrays match each other. Then echo/print out each matching value found and which arrays had those matches.

Here is an example to better describe the problem
(although unlike this my arrays will have about 100 values):

$array1 = array(0=>"random1", 1=>"random2");
$array2 = array(0=>"random2", 1=>"random0");
$array4 = array(0=>"random6", 1=>"random1");
$array5 = array(0=>"random5", 1=>"random2");
$array6 = array(0=>"random4", 1=>"random1");
$array7 = array(0=>"random2", 1=>"random6");
$array8 = array(0=>"random7", 1=>"random9");
$array9 = array(0=>"random8", 1=>"random3");
$array10 = array(0=>"random9", 1=>"random0");

Now how can I echo out each match found when comparing all values in the arrays and what arrays that those matches came from?

I have tried a few ways, but the way I use a foreach and in_array its not going to come out the way I like. With my script I am echoing out the array value and the two arrays that match, then latter on if the same value is matching in another array - the value will echo out again with the other two arrays that had that value match. Instead I want the value and all arrays that have matches for that value, but don't want to echo out all values in each array.

Here is an example of my foreach in_array script:

$xmarker = "X";
echo '<br><table cellpadding="5" cellspacing="0" border="1" style="border-collapse:collapse">';
echo '<tr><td>Value</td><td>array1</td><td>array2</td><td>array3</td><td>array4</td><td>ect...</td></tr>';

foreach ($array1 as $key => $value)
{
$x1=NULL;
$x2=NULL;
$x3=NULL;
$x4=NULL;
if (in_array($value, $array2))
{
$x1=$xmarker;
$x2=$xmarker;
}
if (in_array($value, $array3))
{
$x1=$xmarker;
$x3=$xmarker;
}
if (in_array($value, $array4))
{
$x1=$xmarker;
$x4=$xmarker;
}
if (isset($x1)) { echo "<tr><td>$value</td><td>$x1</td><td>$x2</td><td>$x3</td><td>$x4</td><td>ect...</td></tr>"; }
}

foreach ($array2 as $key => $value)
{
$x1=NULL;
$x2=NULL;
$x3=NULL;
$x4=NULL;
if (in_array($value, $array3))
{
$x2=$xmarker;
$x3=$xmarker;
}
if (in_array($value, $array4))
{
$x2=$xmarker;
$x4=$xmarker;
}
if (isset($x1)) { echo "<tr><td>$value</td><td>$x1</td><td>$x2</td><td>$x3</td><td>$x4</td><td>ect...</td></tr>"; }
}

//ect...

echo "</table><br>";

I hope that helps, the script is not completed but can give you an idea - it might have some errors in it but I don't want to use this script its just to show you how I don't want it done.

Thanks in advance for your help!

cameraman

4:52 pm on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you looked at any of the array intersect [php.net] functions?

webdesigner

5:50 pm on Nov 16, 2007 (gmt 0)

10+ Year Member



Thought about that but I am not sure how I would be able to know which arrays are intersecting. Any ideas?

cameraman

6:58 pm on Nov 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If each array had unique keys you could identify them that way. As you've laid it out, you could compare them two at a time.

I can think of a couple of ways to do it, one where you have an array of the arrays, and the other where you have an array of the array names. Walk through the array, comparing two arrays at a time. You only need to walk forward. Here's the array of names method - the array of arrays is simpler so you can probably figure it out from this:

$arys = array('array1','array2','array4'....); // you skipped 3
$c = count($arys);

for($i = 0, $u = count($arys)-1;$i < $u; $i++) {
for($j = $i + 1; $j < $c; $j++) {
$dups = array_intersect( ${$arys[$i]} , ${$arys[$j]} );
if(count($dups)) { // the docs don't say anything about returning false, but you may have to adjust this
echo "Duplicates in {$arys[$i]} and {$arys[$j}:\n";
print_r($dups);
}
}

This is making use of 'variable variables' (you may see exactly what's going on, so I'm explaining for anyone who looks at it and goes huh?!?).

In the first run, $arys[$i] is "array1" and $arys[$j] is "array2".
So ${'array1'} is the same as $array1 and ${'array2'} is the same as $array2, so we're accomplishing:
array_intersect($array1,$array2)

We're using a nested loop because we want to check array1 against all its fellows, then array2 against all its remaining fellows (it's already been checked with the one before it), etc.

I didn't execute this, so I apologize in advance for any syntax errors.

webdesigner

1:10 am on Nov 17, 2007 (gmt 0)

10+ Year Member



Thanks thats a great idea, but we're almost there.

I ran the script and fixed the errors and but still need some of the matches to ad together and I need the data to display in a table like this (the numbers stand for the array number and the "_" means a space the message board will not allow blanks):

____________1______2_______3______4__ect...
random5_____X______X______________X_____________
random13___________X_______X____________________
random45____X______X______________X_____________
random56____X_____________________X_____________
ect...

Here is the script so far:
(arrays would have more values)

$array1 = array(0=>"random1", 1=>"random2");
$array2 = array(0=>"random2", 1=>"random0");
$array3 = array(0=>"random2", 1=>"random0");
$array4 = array(0=>"random6", 1=>"random1");
$array5 = array(0=>"random5", 1=>"random2");
$array6 = array(0=>"random4", 1=>"random1");
$array7 = array(0=>"random2", 1=>"random6");
$array8 = array(0=>"random7", 1=>"random9");
$array9 = array(0=>"random8", 1=>"random3");
$array10 = array(0=>"random9", 1=>"random0");

$arys = array($array1,$array2,$array3,$array4,$array5,$array6,$array7,$array8,$array9,$array10);
$c = count($arys);

for($i = 0, $u = count($arys)-1;$i < $u; $i++) {
for($j = $i + 1; $j < $c; $j++) {
$dups = array_intersect( $arys[$i] , $arys[$j] );
if(count($dups)) {echo "Duplicates in ".($i+1)." and ".($j+1).":<br>";}
foreach ($dups as $key => $value) { echo $value."<br>"; }
echo "<p>";
}
}

--------
That echos out:

Duplicates in 1 and 2:
random2

Duplicates in 1 and 3:
random2

Duplicates in 1 and 4:
random1

Duplicates in 1 and 5:
random2

Duplicates in 1 and 6:
random1

ect....