Matching arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
softcode
Forum Newbie
Posts: 10
Joined: Tue Sep 18, 2007 4:50 am

Matching arrays

Post 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 !!!
Last edited by softcode on Tue Sep 18, 2007 7:34 am, edited 1 time in total.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

I don't think many members will appreciate being asked to "just write up an algorithm" for you.
Paw
Forum Newbie
Posts: 20
Joined: Tue Jul 17, 2007 10:27 am

Re: Matching arrays

Post 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.
softcode
Forum Newbie
Posts: 10
Joined: Tue Sep 18, 2007 4:50 am

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

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

var_dump(!count(array_diff($arrayA, $arrayB)));
:wink:
softcode
Forum Newbie
Posts: 10
Joined: Tue Sep 18, 2007 4:50 am

Post 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');
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
Post Reply