比较两个多维数组和未设置的匹配元素

UPDATE: After the replies I got I realized I shoud be trying to solve this already with the database query so I wrote a more detailed post here

ORIGINAL POST: I would like to compare two multidimensional arrays and get rid of the elements that match a certain criteria. I know I will have to loop through the arrays with some keys and then unset, but I can't seem to do it properly.

The two arrays are $all which has stored all available rooms and their beds and $reserved which has only reserved rooms and the reserved beds.

I want to loop through all the reservations and take the room title which is on position $reservations[x][0] where x is the currently viewed reservation and compare it with all elements in $all[a][0] where a is the currently viewed room.

So then when I find that value of $all[0][0] => 'Luxury Room' matches with $reservations[0][0] => 'Luxury Room' I will look at the beds and a bed code on position y where y is the currently viewed bed code $reservations[x][1][y] and compare it with all available beds for the matched room so with $all[0][1][b] where b are all the available rooms.

And when I find out that value of $all[0][1][1]=>'xx2' matches the value in $reservations[0][1][0]=>'xx2' I will unset index 01 from $all

so finally when I will loop through the $all array and would list each element's index [0] as title and elements of the array on index 1 as beds I would only get bed 'xx2' as available for the 'Luxury Room'

//$all is an array where index 0 is an array   
$all = array( 0=>array(
                    //index 0 has value 'Luxury Room' (room title)
                    0=>'Luxury Room',
                    //index 1 is an array
                    1=>array(
                            //where index 0 has value 'xx1' (bed code)
                            0=>'xx1',
                            //where index 1 has value 'xx2' (bed code)
                            1=>'xx2')),
            //again index 1 is an array etc. just as above...
            1=>array(
                    0=>'Cheap Room',
                    1=>array(
                            0=>'zz1',
                            1=>'zz2',
                            2=>'zz3',
                            3=>'zz4')));

$reserved = array( 0=>array(
                    0=>'Luxury Room',
                    1=>array(0=>'xx2')));

Use nested loops:

foreach ($all as &$room) {
    foreach ($reserved as $r) {
        if ($room[0] == $r[0]) { // same types of room
            foreach ($room[1] as $i => $code) {
                if (in_array($code, $r[1])) {
                    unset($room[1][$i]);
                }
            }
        }
    }
    $room[1] = array_values($room[1]); // Reset array indexes
}

The $all loop uses a reference for the iteration variable so that the unset() calls modify the original array.

DEMO