duanjian3920 2016-02-19 23:11
浏览 39
已采纳

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

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')));
  • 写回答

1条回答 默认 最新

  • dongsui0929 2016-02-19 23:36
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧