How to compare 2 Arrays

Compare 2 arrays for mathcing values

Page 1 of 1

11 Replies - 933 Views - Last Post: 19 March 2010 - 07:23 AM Rate Topic: -----

#1 mjag16  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 4
  • Joined: 23-February 10

How to compare 2 Arrays

Posted 16 March 2010 - 10:18 AM

$Carton_array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24);
$DB_Carton_array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22);

			$p = 0;
			foreach ($Carton_array as $value) {
				
				if ($value == $DB_Carton_array[$p]) {
					
					echo $value." ";
					$p++;
					
				} else {
					
					echo 'no ';
					
				}
				
			}


and yes - it's that simple - less is more people.

This post has been edited by JackOfAllTrades: 19 March 2010 - 09:50 AM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? -2
  • +

Replies To: How to compare 2 Arrays

#2 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 343
  • View blog
  • Posts: 1,189
  • Joined: 02-April 09

Re: How to compare 2 Arrays

Posted 16 March 2010 - 11:35 AM

If $DB_Carton_array has fewer elements than $Carton_array you will generate an undefined index error.

This works:
if ($DB_Carton_array == $Carton_array)
{
    echo "they are the same";
}
else
{
    echo "they are not the same";
}

and what'd'ya know, it really is that simple.
Was This Post Helpful? 0
  • +
  • -

#3 mjag16  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 4
  • Joined: 23-February 10

Re: How to compare 2 Arrays

Posted 16 March 2010 - 11:48 AM

sorry - but you're wrong - if you actually run the code you will see that it works. nice try though.

and your code doesnt compare each element in the array, just the array itself. thats not very helpful if you want to know if array elements are matching or not.
Was This Post Helpful? -2
  • +
  • -

#4 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 343
  • View blog
  • Posts: 1,189
  • Joined: 02-April 09

Re: How to compare 2 Arrays

Posted 16 March 2010 - 11:52 AM

View Postmjag16, on 16 March 2010 - 05:48 PM, said:

sorry - but you're wrong - if you actually run the code you will see that it works. nice try though.


1 2 3 4 5 6 7 8 9 no 11 12 13 14 15 16 17 18 19 20 no 22 
Notice: Undefined offset: 20 in /var/www/test/index.php on line 123
no
Notice: Undefined offset: 20 in /var/www/test/index.php on line 123



View Postmjag16, on 16 March 2010 - 05:48 PM, said:

and your code doesnt compare each element in the array, just the array itself. thats not very helpful if you want to know if array elements are matching or not.

No, I know it doesn't. It compares the arrays to see if they contain all the same elements. Your post had no context to say otherwise. If it did, I'd probably just use array_diff().
Was This Post Helpful? 0
  • +
  • -

#5 mjag16  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 4
  • Joined: 23-February 10

Re: How to compare 2 Arrays

Posted 16 March 2010 - 11:56 AM

whatever dude - i'm not going to argue - i dont get any errors - but then again i know how to acutally catch them. this is a basic example for students - and i'm trying not to be overly complicated. so back off nobody cares about your array_diff
Was This Post Helpful? -5
  • +
  • -

#6 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: How to compare 2 Arrays

Posted 17 March 2010 - 02:58 AM

View Postmjag16, on 16 March 2010 - 01:56 PM, said:

i dont get any errors


You've probably got your error reporting set to something like E_ALL & ~E_NOTICE which is the most common error reporting setting, as it reports all errors except notices (which is what 'undefined offset' is). Putting error_reporting(E_ALL); at the start of your script will give you the undefined offset notices mentioned above.

This post has been edited by Valek: 17 March 2010 - 02:59 AM

Was This Post Helpful? 1
  • +
  • -

#7 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 717
  • Joined: 03-August 06

Re: How to compare 2 Arrays

Posted 17 March 2010 - 07:30 AM

The proper way to compare two arrays in PHP is to use the array_diff functions: array_diff, array_diff_assoc, array_diff_key, array_diff_uassoc, and/or array_diff_ukey. Your example mjag16 is not a case of less is more. Its a case of poorly coded. So someone does care and array_diff is THAT simple. For a first time poster on here learn some manners.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3715
  • Posts: 13,474
  • Joined: 08-August 08

Re: How to compare 2 Arrays

Posted 17 March 2010 - 07:42 AM

View Postgrimpirate, on 17 March 2010 - 09:30 AM, said:

For a first time poster on here learn some manners.

Quoted for emphasis.

It's better to receive requested help with some humility and tact!
Was This Post Helpful? 0
  • +
  • -

#9 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 544
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: How to compare 2 Arrays

Posted 17 March 2010 - 09:23 AM

View PostCTphpnwb, on 17 March 2010 - 09:42 AM, said:

It's better to receive requested help with some humility and tact!


Would make sense, but he didn't request help. He just came in and posted a thread basically trying to tell people how to do something they never asked to know how to do. Not to mention going about it the wrong way, and with an attitude to boot.

So the help in this thread wasn't requested. It was given in the hope that he might learn where his code is failing.
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3715
  • Posts: 13,474
  • Joined: 08-August 08

Re: How to compare 2 Arrays

Posted 17 March 2010 - 09:50 AM

View PostValek, on 17 March 2010 - 11:23 AM, said:

Would make sense, but he didn't request help. He just came in and posted a thread basically trying to tell people how to do something they never asked to know how to do. Not to mention going about it the wrong way, and with an attitude to boot.

Well then, there's another lesson for him! ;)
Posting the wrong way (no code tags and a poor explanation of your purpose) is a good way to ensure that little (or negative) attention is paid to the substance of your post, be it a request for help or not.
Was This Post Helpful? 0
  • +
  • -

#11 Xuhybrid  Icon User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 106
  • Joined: 20-January 10

Re: How to compare 2 Arrays

Posted 19 March 2010 - 05:03 AM

View Postmjag16, on 16 March 2010 - 10:56 AM, said:

whatever dude - i'm not going to argue - i dont get any errors - but then again i know how to acutally catch them. this is a basic example for students - and i'm trying not to be overly complicated. so back off nobody cares about your array_diff

array_diff() is how you do it.
Idiot! Get lost!
Was This Post Helpful? 1
  • +
  • -

#12 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3715
  • Posts: 13,474
  • Joined: 08-August 08

Re: How to compare 2 Arrays

Posted 19 March 2010 - 07:23 AM

:batman:
I think the point has been made. No need to continue...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1