jimfog Posted February 23, 2015 Share Posted February 23, 2015 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 More sharing options...
davej Posted February 23, 2015 Share Posted February 23, 2015 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 More sharing options...
jimfog Posted February 23, 2015 Author Share Posted February 23, 2015 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 More sharing options...
justsomeguy Posted February 23, 2015 Share Posted February 23, 2015 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 More sharing options...
davej Posted February 23, 2015 Share Posted February 23, 2015 Yes, please describe what you actually need to determine. Link to comment Share on other sites More sharing options...
jimfog Posted February 23, 2015 Author Share Posted February 23, 2015 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. .If the second array has one member than it must be checked if this member are james or john...and output true if yes 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 More sharing options...
justsomeguy Posted February 23, 2015 Share Posted February 23, 2015 You can use in_array to check if an item is in an array. You can also use array_intersect to get the intersection between 2 arrays, or array_diff to get the difference. Link to comment Share on other sites More sharing options...
davej Posted February 23, 2015 Share Posted February 23, 2015 You could do this the old fashioned way, but as JSG says, Php has a lot of built-in methods for arrays that you should study...See...http://php.net/manual/en/ref.array.php Link to comment Share on other sites More sharing options...
jimfog Posted February 24, 2015 Author Share Posted February 24, 2015 I did my job with in_array after all..thanks Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now