Forum Moderators: coopster

Message Too Old, No Replies

function for compare arrays

         

Psychopsia

5:37 pm on Dec 1, 2006 (gmt 0)

10+ Year Member



Hi all!

I was looking for a function to compare two arrays, but I don't found anything useful, so I written this function, if someone needs it.

Usage:

This function is recursive.

(array) $needle: This array will be searched in the haystack.
(array) $haystack: Source array.
(bool) $match_all: If you want to match all values or just the first correct match, default is true.

If both arrays are empty the return is false.

function array_compare($needle, $haystack, $match_all = true)
{
if (!is_array($needle) ¦¦ sizeof($haystack) > sizeof($needle))
{
return false;
}

$count = 0;
$result = false;
foreach ($needle as $k => $v)
{
if (!isset($haystack[$k]))
{
if ($match_all)
{
return false;
}
continue;
}

if (is_array($v))
{
$result = array_compare($v, $haystack[$k], $match_all);
}

$result = ($haystack[$k] === $v) && (($match_all && (!$count ¦¦ $result)) ¦¦!$match_all) ¦¦ (!$match_all && $result);
$count++;
}

return $result;
}

Example:

$a = array('a', 'b');
$b = array('a', 'c');

$r1 = array_compare($b, $a); // false
$r2 = array_compare($b, $a, false); // true

$a = array('a' => 'a', 'b' => 'c');
$b = array('a' => 'z', 'b' => 'c');

$r1 = array_compare($b, $a); // false
$r2 = array_compare($b, $a, false); // true

Complex example:

$a = array(
'a' => array(
'aa' => array('bb' => array('a' => 'av', 'bsuu' => array(';' => 2, 'l' => 4)), 'zz' => 5, 'cc' => 9)
)
);

$b = array(
'a' => array(
'aa' => array('bb' => array('a' => 'av', 'bsuu' => array(';' => 2, 'l' => 4)), 'zz' => 5, 'cc' => 9)
)
);

$c = array_compare($b, $a);
var_dump($c);

[edited by: Psychopsia at 5:40 pm (utc) on Dec. 1, 2006]

coopster

7:06 pm on Dec 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Wouldn't array_intersect [php.net] or array_diff [php.net] work?

Psychopsia

7:59 pm on Dec 1, 2006 (gmt 0)

10+ Year Member



No, because the purpose is verify if two arrays are the same in all keys and values, not the difference between both.

I use this function in a member auth level system, example the $a is data stored in the database and $b is the array of what I want to check if they have access for each page.

coopster

8:15 pm on Dec 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Did you realize you could just do a straight comparison?
$array1 = array( 
'1' => 'one',
'2' => 'two',
'3' => 'three'
);
$array2 = array(
'1' => 'one',
'2' => 'two',
'3' => 'three'
);
if ($array1 == $array2) {
print 'TRUE';
} else {
print 'FALSE';
}

... will print TRUE. Now, if the keys in the first array were numeric instead of strings, it will still print TRUE. It doesn't compare types, but I don't think your example would either. Just something to note though.
Comparison Operators [php.net]

I hope you don't think I am picking on your here, Psychopsia, I'm not. I'm just pushing the discussion a little to see what we can discover!

Psychopsia

8:45 pm on Dec 1, 2006 (gmt 0)

10+ Year Member



Yes I know it's easier to compare arrays with ==, but for the auth level purposes, if you want to get a specific option this function can be useful. Maybe there's another way to do it, so hope to get a good discussion here. :)

In the following I can't use straight comparison, check if user can submit tickets, $a is decoded array from database for the current user:

$a = array(
'tickets' => array(
'search' => array(
'a' => true
),
'submit' => true,
'view' => array(
'self' => true,
'others' => true
)
)
);

$b = array(
'tickets' => array(
'submit' => true
)
);

if (array_compare($a, $b, false))
{
// Show button
}

Note: The function check the type of values

$result = ($haystack[$k] === $v)...

[edited by: Psychopsia at 8:55 pm (utc) on Dec. 1, 2006]

Psychopsia

9:22 pm on Dec 3, 2006 (gmt 0)

10+ Year Member



Do you think my last example is a good way to do that?
Of course, the permission tree has more childs.

coopster

3:38 pm on Dec 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I don't have my head completely wrapped around the way your are doing your permissions but if it is working for you, so be it.