Compare multidimentional arrays
Posted: Fri Jul 29, 2005 7:34 am
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:
In the manual it says that arrays are compared as follows:
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...
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:
But this article were from May 30, 2003, so maybe the statement isn't relevant anymore.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.
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
}
?>Can anybody give me a clear answer on this (I'm using PHP 5.0.3)
regards tores...