简体   繁体   English

在PHP中过滤多维数组

[英]Filter a multidimensional array in PHP

I have this array containing my bookings: 我有包含我的预订的数组:

$bookings[] = array(
    'booking_id' => '1', 
    'client_firstname' => 'Gareth', 
    'days' => array(
        array('day_id' => '2016-11-23,2016-11-24', 'room_id' => '2'),
        array('day_id' => '2016-11-25', 'room_id' => '4'),
        array('day_id' => '2016-11-26,2016-11-27,2016-11-28', 'room_id' => '2'),
    )
);
$bookings[] = array(
    'booking_id' => '2', 
    'client_firstname' => 'James', 
    'days' => array(
        array('day_id' => '2016-11-25,2016-11-26,2016-11-27,2016-11-28', 'room_id' => '5')
    )
);
$bookings[] = array(
    'booking_id' => '2', 
    'client_firstname' => 'Marco', 
    'days' => array(
        array('day_id' => '2016-11-24', 'room_id' => '5')
    )
);

Explication of the array: 数组的说明:

  • The array shows the booking_id and the client_name . 该数组显示booking_idclient_name
  • The array days contains the schedule of the client in the hotel. 排列days包含客户在酒店中的时间表。 Each array in this days array explains which days and in which room the client is. 这个days数组中的每个days说明了客户在哪几天和在哪个房间。 If I have more than one line, the client change room. 如果我有多条线路,客户会更衣室。

The wish 愿望

For a date I give, I need to get the bookings where into the days array the first date correspond to the date I give. 对于我给出的日期,我需要获取预订,其中第一个日期对应于我给出的日期,进入days array


Example

  • Date I give is 2016-11-25 . 我给的日期是2016-11-25
  • I need to get: 我需要得到:

    $bookings[] = array( 'booking_id' => '1', 'client_firstname' => 'Gareth', 'days' => array( array('day_id' => '2016-11-25', 'room_id' => '4') ) ); $bookings[] = array( 'booking_id' => '2', 'client_firstname' => 'James', 'days' => array( array('day_id' => '2016-11-25,2016-11-26,2016-11-27,2016-11-28', 'room_id' => '5') ) );


What I have try 我尝试了什么

$day_value = '2016-11-23';
$bookings_ = array();
foreach($bookings as $b){
    foreach($b['days'] as $day){
       if(in_array($day_value,explode(',',$day['day_id'][0]))){
         $bookings_[]  = $b;
       }
    }
}
print_r($bookings_); 

But it returns me all the results into the bookings... 但这会将所有结果返回给我...

Could you please help me ? 请你帮助我好吗 ?

Thanks. 谢谢。

What do you think is $day['day_id'][0] ? 您认为$day['day_id'][0]什么?

It's the first symbol of $day['day_id'] as latter is a string. 它是$day['day_id']的第一个符号,因为后者是一个字符串。 So, nothing will be exploded by , in $day['day_id'][0] . 所以,什么都不会被分解,$day['day_id'][0] And solution is to remove [0] : 解决方法是删除[0]

foreach($bookings as $b){
    foreach($b['days'] as $day){
        if(in_array($day_value,explode(',',$day['day_id']))){
            // this string added:
            $b['days'] = $day;

            $bookings_[]  = $b;

            // btw, if your `$b['days']` has several elements
            // and in a couple of them there's a required date
            // then `$b` will be added to `$bookings_` several 
            // times, so probably you need `break`
            // break;
        }
    }
}

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

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