比较两个多维数组和取消匹配元素 [英] Compare two multidimensional arrays and unset matched elements
问题描述
更新:之后我得到了我意识到我768,16试图与数据库查询已经解决这个问题的答复,所以我写了<一个href=\"http://stackoverflow.com/questions/35521876/retrieving-available-rooms-and-their-beds-for-a-given-date-range-from-database\">a更详细的岗位这里
原贴:
我想比较两个多维数组,摆脱符合特定条件的元素。我知道我会通过与一些键,然后取消设置阵列具有循环,但我似乎不能正确地做到这一点。
这两个数组是 $所有
已存储的所有可用的房间和床和 $保留
它只有预留的房间和床位保留。
我要遍历所有的保留和这个房间的标题是在位置 $预约[X] [0]
其中x是当前查看的预订和比较它与 $所有元素都[A] [0]
,其中一个是当前查看的房间。
于是当我找到 $的该值的所有[0] [0]
=>豪华间与 $保留匹配[0 ] [0]
=>'豪华客房'我会看床和位置y床上code其中y是当前查看的床code $预约[X] [1] [Y]
并与匹配的房间里所有床位比较所以用 $所有[0] [1] [b]
其中,b是所有可用的房间。
而当我发现 $的该值的所有[0] [1] [1]
=>XX2在 $预约[0] [1] [0]
=>XX2我会取消设置指数<一个href=\"http://stackoverflow.com/questions/35521876/retrieving-available-rooms-and-their-beds-for-a-given-date-range-from-database\">01从 $所有
所以最后的时候我会通过循环 $所有
阵列,并会列出每个元素的索引[0]作为标题和索引的数组元素<一个href=\"http://stackoverflow.com/questions/35521876/retrieving-available-rooms-and-their-beds-for-a-given-date-range-from-database\">1因为单人床我只会让上床XX2作为适用于豪华间
// $都是一个数组,其中索引0是一个数组
$所有=阵列(0 =&GT;阵列(
//指数0的值为豪华间(房间名称)
0 =&GT;豪华间,
//指数1是一个数组
1 =&GT;阵列(
//其中索引0具有价值XX1'(床code)
0 =&GT;'XX1',
//其中指数1的值为XX2(床code)
1 =&GT;'XX2')),
//重新索引1是一个数组等上面一样...
1 =&GT;阵列(
0 =&GT;便宜房,
1 =&GT;阵列(
0 =&GT;'zz1',
1 =&GT;'zz2',
2 =&GT;'zz3',
3 =&GT;'ZZ4')));$保留=阵列(0 =&GT;阵列(
0 =&GT;豪华间,
1 =&GT;阵列(0 =&GT;'XX2')));
使用嵌套循环:
的foreach($所有为&放大器; $房){
的foreach($保留为$ R){
如果($室[0] == $ R [0]){//同一类型的房间
的foreach($室[1] $ I =&GT; $code){
如果(in_array($code,$ R [1])){
未设置($室[1] [$ i]);
}
}
}
}
$室[1] = array_values($室[1]); //重置数组索引
}
的 $所有
循环使用了迭代变量的引用,使未设置()
通话修改原来的数组。
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.
这篇关于比较两个多维数组和取消匹配元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!