Page 1 of 1

Matching arrays

Posted: Tue Sep 18, 2007 5:06 am
by softcode
Hi,

I need to make a comparison between two arrays arrayA and arrayB where

values from array 1 match up to the correct values in array 2 i.e. 1 - 1,2-2 etc

The two arrays contain the same values but not in the same position. Values in arrayA always need to be
matched up in values in arrayB. But arrayB could have more values that A.

arrayA (1,2,3,4,5)

arrayB (4,1,3,5,2)

arrayA arrayB
1 4
2 1
3 3
4 5
5 2

Some light please !!!

Posted: Tue Sep 18, 2007 5:11 am
by aceconcepts
I don't think many members will appreciate being asked to "just write up an algorithm" for you.

Re: Matching arrays

Posted: Tue Sep 18, 2007 6:07 am
by Paw
softcode wrote:Hi,

I need to make a comparison between two arrays arrayA and arrayB where

values from array 1 match up to the correct values in array 2 i.e. 1 - 1,2-2 etc

The two arrays contain the same values but not in the same position. Values in arrayA always need to be
matched up in values in arrayB. But arrayB could have more values that A.

arrayA (1,2,3,4,5)

arrayB (4,1,3,5,2)

arrayA arrayB
1 4
2 1
3 3
4 5
5 2

Could someone just write me up an algorithm or piece of code in php
please?
I'm not sure whether I understood you 100% correctly, what your algorithm needs to do. But as far as I understood, you could do following:
  • Sort both arrays
  • Take a loop, either 'for' or 'while'
  • For each array, take an individual index variable (i, j)
  • Try to match arrayA == arrayB[j], count the matches
  • If arrayA is greater than arrayB[j], increase j - and the other way around, increase i, if arrayA is less than arrayB[j]. Increase both, if the array cell values are equal.
  • Stop the loop, if one of the indices i or j reached the end of the according array


However, I agree with aceconcepts, that programmers don't appreciate coding other people's specific programming problems, especially when they sound like school or university homework. That's not supposed to be some elite meanness. It's actually meant to help people understand their problems better. Your problem is not "how to find someone doing my homework". Your problem is solving your problem and learning by doing so.

So implementing above suggestion is still up to you.

Posted: Tue Sep 18, 2007 7:44 am
by softcode
I need to match the two without sorting any array. So the values should remain as they are. For every value in arrayA,
I have to search through it every value in arrayB.

arrayA (1,2,3,4,5)

arrayB (4,1,3,5,2)


This is not some home work but its part of a bigger system and I really need to know how to do this.
Sorting the two arrays and matching them up is a solution but it won't work well with the rest of the system.

Posted: Tue Sep 18, 2007 7:51 am
by John Cartwright

Code: Select all

$arrayA = array('foo', 'bar', 'foobar');
$arrayB = array('foobar', 'bar', 'foo');

var_dump(!count(array_diff($arrayA, $arrayB)));
:wink:

Posted: Wed Sep 19, 2007 3:29 am
by softcode
Thanks Jcart,

But for the system to work correctly what is required is that foo from arrayA is matched aganist all the elements of arrayB - foobar,bar,foo - until it matches correctly with foo where it returns true.the same with all other array values of arrayA.

$arrayA = array('foo', 'bar', 'foobar');
$arrayB = array('foobar', 'bar', 'foo','kaa');

Posted: Wed Sep 19, 2007 4:17 am
by VladSun