简体   繁体   English

使用PHP检查并删除多维数组中的数组值

[英]check and remove the array values in Multi-Dimensional array with PHP

I designed the system for booking rooms and store the data by multi-dimensional array form. 我设计了用于预订房间的系统,并以多维数组形式存储数据。 Now I would like to code the function about finding the array values which duplicating on the time and date (from, to, date). 现在,我想对有关查找在时间和日期(从,到,日期)上重复的数组值的函数进行编码。

The logic is a room can be booked one time or more in one day but it cannot booked by one or more users by the same period. 逻辑是一间房间可以在一天中预订一次或多次,但不能在同一期间内由一个或多个用户预订。 For example, John booked 1300-1500 in 15/08/2014 so that no one can book on this period anymore. 例如,约翰在2014年8月15日预订了1300-1500,因此在此期间没有人可以预订。

I don't know how to implement the function about removing those values while checking although I can understand the logic. 尽管我能理解逻辑,但我不知道如何实现有关在检查时删除这些值的功能。

Here is the array (data): 这是数组(数据):

    $recordBooking = array(
            "112"=>array(
                "date"=>array(
                    "24/09/2014"=>array(
                        array(
                            "from"=>1,
                            "to"=>3,
                            "username"=>"Amy"
                                ),
                        array(   //duplicate data 
                            "from"=>2,
                            "to"=>5,
                            "username"=>"John"
                                )   
                        ),
                    "27/09/2014"=>array(
                        array(
                            "from"=>1,
                            "to"=>3,
                            "username"=>"Chars"
                                )
                        ),
                    "29/09/2014"=>array(
                        array(
                            "from"=>1,
                            "to"=>5,
                            "username"=>"Peter"
                                )
                        ),
                    "30/09/2014"=>array(
                        array(
                            "from"=>3,
                            "to"=>6,
                            "username"=>"Amy"
                                )
                        )
                    )
                )
        );

I want to set the alert message and remove the values which is duplicate when the function is detected the repeat data. 我想设置警报消息并删除检测到该功能的重复数据时重复的值。 Thanks for any help. 谢谢你的帮助。

For array filtering there is an array_filter function invented. 为了进行数组过滤,发明了一个array_filter函数。 Let's assume you are already iterating dates: 假设您已经在迭代日期:

$booked = []; // holder for already booked
$result = array_filter(
  $recordBooking['112']['date']['24/09/2014'], // array value when iterating dates
  function($rb) use (&$booked) { // will filter this date

    foreach($booked as $b) {
      if(
        ($b['from'] < +$rb['from'] && $b['to'] > +$rb['from']) ||
        ($b['from'] < +$rb['to'] && $b['to'] > +$rb['to'])
      ) { // already booked!!!
        echo "ALERT. Skipped: ${rb['username']}\n";
        return false;
      }
    }
    $booked[] = ['from' => +$rb['from'], 'to' => +$rb['to'] ]; // mark time as booked
    return true;
  }
);

var_dump($result); 

// ⇒
/*
ALERT. Skipped: John
array(1) {
  [0]=>
  array(3) {
    ["from"]=>
    int(1)
    ["to"]=>
    int(3)
    ["username"]=>
    string(3) "Amy"
  }
}
*/

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM