Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php,我如何选择天气正好在今天的数组';谁的日期?_Php_Arrays - Fatal编程技术网

Php,我如何选择天气正好在今天的数组';谁的日期?

Php,我如何选择天气正好在今天的数组';谁的日期?,php,arrays,Php,Arrays,我得到了一个这样的数组: Array ( [0] => Array ( [dt] => 1523728800 [main] => Array ( [temp] => 16.84 [temp_min] => 15.73 [temp_max] => 16.84 [pressure] => 1001.67

我得到了一个这样的数组:

Array
      (
     [0] => Array
(
    [dt] => 1523728800
    [main] => Array
        (
            [temp] => 16.84
            [temp_min] => 15.73
            [temp_max] => 16.84
            [pressure] => 1001.67
            [sea_level] => 1036.08
            [grnd_level] => 1001.67
            [humidity] => 66
            [temp_kf] => 1.11
        )

    [weather] => Array
        (
            [0] => Array
                (
                    [id] => 800
                    [main] => Clear
                    [description] => clear sky
                    [icon] => 01n
                )

        )


    [dt_txt] => 2018-04-14 18:00:00
)

[1] => Array
    (
        [dt] => 1523739600
        [main] => Array
            (
                [temp] => 12.97
                [temp_min] => 12.23
                [temp_max] => 12.97
                [pressure] => 1002.14
                [sea_level] => 1036.93
                [grnd_level] => 1002.14
                [humidity] => 67
                [temp_kf] => 0.74
            )

        [weather] => Array
            (
                [0] => Array
                    (
                        [id] => 800
                        [main] => Clear
                        [description] => clear sky
                        [icon] => 01n
                    )

            )

        [dt_txt] => 2018-04-14 21:00:00
    )
还有36个。对于8个阵列,每天的天气步长为3小时


我如何选择与今天天气完全一致的阵列?如您所见,返回的日期-
dt_txt
,但如何正确搜索它?

您可以循环数组并使用给定的unix时间进行检查:

$today_weather = array();
$today = date('Ymd');
foreach($data as $weather){
    // Check if the date is today
    if($today === date('Ymd', $weather['dt'])){
        $today_weather[] = $weather;
    }
}

编辑:这会将今天的天气添加到
$today\u weather

您可以轻松使用数组中的“dt”项,并将其与当天的Unix秒数范围进行比较

$daystart = strtotime(date("Y-m-d 00:00:00"));
$dayend = strtotime(date("Y-m-d 23:59:59"));
Foreach($arr as $val){
    If($val['dt'] >= $daystart && $val['dt'] <= $dayend){
        $res[] = $val;
    }
}
Var_dump($res);
$daystart=strottime(日期(“Y-m-d 00:00:00”);
$dayend=strottime(日期(“Y-m-D23:59:59”);
外汇($arr作为$val){

如果($val['dt']>=$daystart&&$val['dt']你能把数组作为PHP代码包含进来吗?也可以发布你到目前为止尝试过的代码。你可以迭代数组并检查
dt
索引。
dt
是unix时间。数组中的时间有一个固定的基数?我的意思是像每2小时或3小时一样,API没有选择特定日期的选项?@chris85,这个API有选择某一天的选项,但它是付费的:c I
我只是在制作TelegrameWeatherBot。这个API-openweathermapI已经尝试过了,但我需要得到那些带有今天日期的数组,但不仅仅是检查日期:c@БіПач我不确定你的意思。如果你能检查元素是否有今天的日期,那么你可以做任何事情你想用它。@БіПач如果你的意思是创建一个今天的天气数组,检查更新后的答案,尽管这很明显。Mehdi,OP希望你对整个问题进行编码Lolool。我这周遇到了一些似乎无法建立答案的问题。我大多数被接受的答案都是我为OP写的。总之,OP,是的-您只需将日期添加到数组。@Nerdi.org现在我明白了。他还有很多未解决的问题。请检查: