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 !!!
Matching arrays
Moderator: General Moderators
Matching arrays
Last edited by softcode on Tue Sep 18, 2007 7:34 am, edited 1 time in total.
- aceconcepts
- DevNet Resident
- Posts: 1424
- Joined: Mon Feb 06, 2006 11:26 am
- Location: London
Re: Matching arrays
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: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?
- 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.
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.
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
$arrayA = array('foo', 'bar', 'foobar');
$arrayB = array('foobar', 'bar', 'foo');
var_dump(!count(array_diff($arrayA, $arrayB)));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');
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');