Compare multidimentional 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
tores
Forum Contributor
Posts: 120
Joined: Fri Jun 18, 2004 3:04 am

Compare multidimentional arrays

Post by tores »

Hi

Is it possible to test if a couple of two-dimentional arrays is equal with the === operator?
After doing some testing it seems to me that it is... However, as I know that this is not that case in any of the
other programminglanguages I know, I made a search on the net. What I found was an article stating that:
PHP knows how to compare two numbers or two text strings, but in a multidimensional array, each element is an array. PHP does not know how to compare two arrays, so you need to create a method to compare them.
But this article were from May 30, 2003, so maybe the statement isn't relevant anymore.
In the manual it says that arrays are compared as follows:

Code: Select all

<?php
// Arrays are compared like this with standard comparison operators
function standard_array_compare($op1, $op2)
{
   if (count($op1) < count($op2)) {
       return -1; // $op1 < $op2
   } elseif (count($op1) > count($op2)) {
       return 1; // $op1 > $op2
   }
   foreach ($op1 as $key => $val) {
       if (!array_key_exists($key, $op2)) {
           return null; // uncomparable
       } elseif ($val < $op2[$key]) {
           return -1;
       } elseif ($val > $op2[$key]) {
           return 1;
       }
   }
   return 0; // $op1 == $op2
}
?>
But is this only for single-dimention arrays or is it also for multi-dimentional arrays (recursive)
Can anybody give me a clear answer on this (I'm using PHP 5.0.3)

regards tores...
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

an identical check (===) would be a very good idea, but that is only if the values are exactly identical.

ie : $mymulti[0][1]['key'] === $mymulti2[0][1]['key']

both would have the value 'bob'

if one has 'Bob' and the other 'bob', it will no longer be considered identical and return false.

this is a good practice though because you will want to text for identical values before proceeding.

so, this ...['key'] == ...['key] will return true even if the values are 005, 5

but yeah, you can definately do that.

i use ereg but it's all the same

Code: Select all

foreach($array as $key => $value) {
  if(ereg($key,$myOtherArray)) {
    if($myOtherArray === $key) {
      return TRUE;
    }
  }
}
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

Comparing two multidimentional arrays is like digging deep and see if the two can match each others child values.

Code: Select all

function CompareArrays($aray1,$aray2){
	if(count($aray1)!=count($aray2)) return false;
	for($a=0;$a<count($aray1);$a++){
		if($aray1[$a]!=$aray2[$a]) return false;		
		if(is_array($aray1[$a])){
			if(!CompareArrays($aray1[$a],$aray2[$a])) return false;
		}			
	}
	return true;
}
// test
$aray1 = array(
	array(
		array(45,32,1),
		array(0,0)),
	array(20,43),
	100);
$aray2 = array(
	array(
		array(45,32,1),
		array(0,0)),
	array(20,43),
	4); // <-- the diference	

if(CompareArrays($aray1,$aray2))
	echo "Matched";
else 	
	echo "Not Matched";

// must output 'Not Matched'
Post Reply