tags:

views:

228

answers:

2

I have the following array, named $usergrouppermissions:

Array
(
    [0] => Array
        (
            [UserGroupPermission] => Array
                (
                    [group_id] => 1
                    [permission_id] => 4
                )

        )

    [1] => Array
        (
            [UserGroupPermission] => Array
                (
                    [group_id] => 1
                    [permission_id] => 5
                )

        )

    [2] => Array
        (
            [UserGroupPermission] => Array
                (
                    [group_id] => 1
                    [permission_id] => 6
                )

        )

    [3] => Array


[...]

In a loop, i build another array like this, named $searchme:

Array
(
    [UserGroupPermission] => Array
        (
            [permission_id] => 1
            [group_id] => 1
        )

)

Now, i want to check, if given array above exists in the numeric-indexed array at the top. I currently do this:

$result = Set::contains($usergrouppermissions, $searchme);

The result is always false. Do i get something wrong here? What is the problem?

A: 

Since PH 4.2 in_array can also use arrays as needle. So, the solution is this:

in_array($searchme, $usergrouppermissions)
d1rk
A: 

I know I'm very late with this answer, but here it is anyway for future generations :)

The cake way would be:

$exists = Set::matches
    (
     sprintf
     (
      '/UserGroupPermission[group_id=%s][permission_id=%s]',
      $searchme['UserGroupPermission']['group_id'],
      $searchme['UserGroupPermission']['permission_id']
     ),
     $usergrouppermissions
    );

If you take a look at the code of Set::contains() you will see it doesn't work well with nested contains (at least not in your case).

Set::matches() basically just calls Set::extract() with the XPath condition given and turns the result into a bool value for your pleasure.

dr Hannibal Lecter