【问题标题】:check if variable is higher than item in array php检查变量是否高于数组php中的项目
【发布时间】:2016-04-08 16:28:07
【问题描述】:

我想检查一个变量是否大于或等于数组中的一项。

我尝试了多个 for 循环,但它们只是堆叠、堆叠和堆叠。我只需要获取日期,例如:01-01-2005 / 01-01-2006 并将这两者与今天的日期进行比较,并检查今天的日期是否大于或等于书的日期。如果这本书较旧,那么今天的日期做一些代码

foreach ($user_out as $userOut) {
    $userOut_id[] = $userOut['user_id'];
    }
    $userOut_id_implode = implode(',',$userOut_id);
//loop through each element from the tocomet able
$dateOfBook = [];
$today = date("d-m-Y");
foreach ($out as $outs) {
    $datum = $outs['released'];

    //gets the date of the selected book.
    $bookDate = date("d-m-Y", strtotime($datum));
    $dateOfBook[] = $outs['released'];

    //gets the name of the author
    $author_name = $outs['author'];

    //gets the title of the book
    $book_title = $outs['book_title'];

    $today = strtotime($today);
    $bookDate = strtotime($bookDate);
    //checks if today is equal or smaller then the bookdate 

}
$today = date("Y-m-d");
$y = count($dateOfBook);

for($x = 0; $x < $y; $x++){
    echo $x;
};

if($today >= $dateOfBook($x)){
    do stuff
}

【问题讨论】:

  • $dateOfBook 只需访问您要比较的元素:$dateOfBook[1]
  • 但我想将所有这些都与 $today 进行比较。
  • @AngelofDemons 为什么不使用 for 或 foreach 循环?
  • 再做一个循环。 foreach($dateOfBook as $xpto)
  • 如果您只想要今天之前发行的书籍,请尝试使用数据库为您过滤。 where released &lt;= CURDATE() ...

标签: php arrays compare


【解决方案1】:
$dateOfBook = [];
$today = date("d-m-Y");
$flag = 0;
foreach ($out as $outs) {

    //gets the date of the book out of the database 
    if($today >= $outs['released']){
        $flag++;
    }
    $dateOfBook[] = $outs['released']; # is this necessary?
}

if($flag){
   # do something
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 2010-10-28
    • 2013-10-14
    相关资源
    最近更新 更多