Jump to content

comparing arrays with a while loop


jimfog

Recommended Posts

I am trying to use a while loop to compare 2 arrays...something like this:

    $i=0;    while($i<count($serviceslist))        {       if(($serviceslist[$i]["serviceID"])!==($content['services'][$i]))        { echo 'hi';}       $i++;        }

There is a problem though....there are time where the 2 arrays are not equal in size.

The service list array might have 2 members and the content array might have 1 member.

 

At such a case when $i=1 then I get an undefined offset warning...something to be expected of course since

the second array has only one member(in this example).

 

How I could compare the 2 arrays and still take into consideration that these may not be of equal size?

Some code adjustment is needed here.

Link to comment
Share on other sites

The answer would depend upon the way the two arrays are organized.

What you are doing now is searching for elements that do not match in corresponding locations. Do you want to ignore the extra elements in the longer array?

array1 = array(1,2,3,4,5,6,7,8,9,10);array2 = array(1,2,3,4,5,6,7,8,9,10,44,55,66);

...or will it be...

array1 = array(1,2,3,4,5,6,7,8,9,10);array2 = array(44,55,66,1,2,3,4,5,6,7,8,9,10);

...with the extra elements at the bottom of the longer array?

 

Or do you really need to search each location of each array?

array1 = array(1,2,3,4,5,6,7,8,9,10);array2 = array(1,5,4,3,2,6,8,7,10,9);
Link to comment
Share on other sites

Do you need to search every element of array1 for every element of array 2?

yes...I want to check for example the first member of the first array with the first member of the second array and so on...I want to focus on the above approach first...
Link to comment
Share on other sites

If you're trying to check if the arrays have the same elements in the same order then the first thing you can do is check their lengths. If they have different lengths then the arrays are not the same and you don't need to loop at all.

Link to comment
Share on other sites

If you're trying to check if the arrays have the same elements in the same order then the first thing you can do is check their lengths. If they have different lengths then the arrays are not the same and you don't need to loop at all.

No...the check must be performed either way...let me explain it better.

 

Suppose the first array has two names in it,james and john...

 

I want to check if in the second array these names or one of them(if the array has only one member) are present in it.

 

The first array will always have 2 members in it...the second might have one or two in it.

  1. .If the second array has one member than it must be checked if this member are james or john...and output true if yes
  2. If the second array has two members it must be checked that both of these are james and john...if one of them is not then output false.

 

To say it differently I want to check the choices the user made with the ones found in the database...as a validation algorithm.

Link to comment
Share on other sites

I did my job with in_array after all..thanks

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...