在数组中查找某些元素具有多个维度的值

I have an array that can contain multidimensional elements. For populating checkboxes from database in the edit-form, I need to check for specific key-value pairs from this array.

The array ($areastools) can look like this:

array:9 [▼
 15 => array:2 [▼
      0 => 1
      1 => 4
 16 => 4
 45 => array:2 [▶]
 47 => 4
 50 => array:2 [▶]
 51 => 4
]

The first key is the specific area id ($area->pivot->id), and the values are the tool ids ($tool->id).

Currently I have this check, where the checkbox is checked if

(array_key_exists($area->pivot->id, $areastools) && 
($areastools[$area->pivot->id] == $tool->id))

This works for all elements of the array that are not multidimensional. The correct checkboxes are then checked in the edit-form.

But when two tools are added to the same area, the checkboxes for none of these tools are checked, as the condition does not check the values correct if an area has many tools.

I have been searching and trying, but can not find out how to do this. Can anyone please help me?

I'd make a little function. Not very elegant, but it works.

function areaTools($areaTools, $areaId, $toolId) {
    if(array_key_exists($areaId, $areaTools) {
        if(is_array($areaTools[$areaId]) {
            foreach($areaTools[$areaId] as $tool) {
                if($tool == $toolId;) {
                    return true;
                }
            }
        } else {
            return $areaTools[$areaId] == $toolId;
        }
    }
}

if you only want to know if a certain key-value pair is found, use array_walk_recursive()

$found=0;
function find_key_value($v, $k)
{
    global $area,$tool,$found;
    if (($k==$area->pivot->id)&&($v==$tool->id))
         $found=1;
}
array_walk_recursive($areastools, 'find_key_value');