foreach循环比较数组并创建仅包含重复值的新数组

时间:2019-11-29 15:30:08

标签: arrays foreach

我正在使用与数据库的连接,并检索每个房号的日期。

数组的输出为

Array ( [0] => 2020-05-23 [1] => 2020-05-24 [2] => 2020-05-25 [3] => 2020-05-26 [4] => 2020-05-27 [5] => 2020-05-28 [6] => 2020-05-29 [7] => 2020-05-30 [8] => 2020-07-23 [9] => 2020-07-24 [10] => 2020-07-25 [11] => 2020-07-26 [12] => 2020-07-27 [13] => 2020-07-28 [14] => 2020-07-29 [15] => 2020-07-30 [16] => 2020-07-31)

Array ( [0] => 2020-05-23 [1] => 2020-05-24 [2] => 2020-05-25 [3] => 2020-05-26 [4] => 2020-05-27 [5] => 2020-05-28 [6] => 2020-05-29 [7] => 2020-05-30 [8] => 2020-07-23 [9] => 2020-07-24 [10] => 2020-07-25 [11] => 2020-07-26 [12] => 2020-07-27 [13] => 2020-07-28 [14] => 2020-07-29 [15] => 2020-07-30 [16] => 2020-07-31 [17] => 2020-08-01 [18] => 2020-08-02 [19] => 2020-08-03 [20] => 2020-08-04 [21] => 2020-08-05 [22] => 2020-08-06 [23] => 2020-08-07 [24] )


Array ( [0] => 2020-05-23 [1] => 2020-05-24 [2] => 2020-05-25 [3] => 2020-05-26 [4] => 2020-05-27)

代码是

foreach($sqlhotels as $myhotel){

        $selectedhotel= $myhotel['roomcode'];

foreach($roomcode as $rcode){

    $connection = mysqli_connect($hostname,$username,$password,$database);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$GLOBALS['started'] = false;

    $statement =  mysqli_query($connection,"select * from icalproperty where roomtypecode = '".$rcode."'order by ipid ASC");

            $this_hotels_dates = array();
                if(mysqli_num_rows($statement) > 0 )
                {
                    while($rowww = mysqli_fetch_assoc($statement)):
                            $this_hotels_dates[] = $rowww;                          
                    endwhile;                                                   

                }               
                else
                {
                    die("No data found.");
                }
    $datesarray = array();
    foreach($this_hotels_dates as $dates)
    {
        $datesarray[] = $dates['dates'];        
    }

$string = implode(",",$datesarray);
$databasedates = explode(",",$string); //disable dates array

print_r($databasedates);    
}       
    }

现在我需要合并这3个数组,并只保留重复的日期,因此新数组将是:

Array ( [0] => 2020-05-23 [1] => 2020-05-24 [2] => 2020-05-25 [3] => 2020-05-26 [4] => 2020-05-27)

0 个答案:

没有答案