【问题标题】:Function to return true from an if conditional nested in a for loop从嵌套在 for 循环中的 if 条件返回 true 的函数
【发布时间】:2020-01-02 16:48:50
【问题描述】:

我遇到了问题,不知道这是否是一个好的解决方案,或者我是否离我很远。 如果到期日期大于今天,我想返回 true 或 false,下面的代码做得很好,除了我不能使用要返回的到期变量,所以我可以使用该值。

任何帮助或此代码有什么问题将不胜感激。 提前致谢。

function fileRange($files){
    $today  =   date('Ymd');
    $expiry_date = '';
    $expiry = '';
    for( $i = 0; $i <= 50; $i++)
    {
        $file = $files[$i];
        $expiry_date    .=   $file['expiry_date'];
        $splited_array = str_split($expiry_date, 8);
        if ($splited_array[$i] > $today){
            $expiry =   true;
        }else{
            $expiry =   false;
        }
        /* var_dump($expiry);
         Here the variable works just fine, returning all     
         the possible combinations but after the for loop
         I can't access all the values but only the last 
         one */
    }

    return $expiry;
}

这是 $file 参数

if (fileRange($files))

这是 $files 参数

[0]=>
 array(2) {
  ["file"]=> array(21) {
            ["ID"]=>  int(2904)
            ["id"]=>  int(2904)
            ["title"]=> string(55) "..."
            ["filename"]=> string(58) "..."
            ["filesize"]=> int(434223)
            ["url"]=> string(106) "..."
            ["link"]=> string(90) "..."
            ["alt"]=>  string(0) ""
            ["author"]=> string(1) "1"
            ["description"]=> string(0) ""
            ["caption"]=> string(0) ""
            ["name"]=> string(53) "..."
            ["status"]=> string(7) "inherit"
            ["uploaded_to"]=> int(615)
            ["date"]=> string(19) "2019-02-19 15:55:20"
            ["modified"]=> string(19) "2019-02-19 15:55:20"
            ["menu_order"]=> int(0)
            ["mime_type"]=> string(15) "application/pdf"
            ["type"]=>  string(11) "application"
            ["subtype"]=> string(3) "pdf"
            ["icon"]=> string(58) "..."
        }
    ["expiry_date"]=>  string(8) "20190802"
}

【问题讨论】:

  • 请向我们展示您在$files 参数上传递的内容
  • 注意:您处于循环中,因此此 $expiry = true; 或此 $expiry = false; 行将在每次循环时覆盖 $expiry
  • 如果您想要所有 50 个结果,您还必须将 $expiry 设为一个数组
  • 从@RiggsFolly 的第一点继续,您可能需要break 退出循环。
  • @RiggsFolly 添加了 $files 参数

标签: php function boolean


【解决方案1】:

只需将您的 $expiry 变量替换为数组

function fileRange($files){
    $today  =   date('Ymd');
    $expiry_date = '';
    $expiry = array();
    for( $i = 0; $i <= 50; $i++)
    {
        $file = $files[$i];
        $expiry_date    .=   $file['expiry_date'];
        $splited_array = str_split($expiry_date, 8);
        if ($splited_array[$i] > $today){
            $expiry[] =   true;
        }else{
            $expiry[] =   false;
        }
    }

    return $expiry;
}

【讨论】:

  • 您也可以将完整的if/else-block 替换为:$expiry[] = splited_array[$i] &gt; $today;。甚至是$expire[] = substr($expiry_date, 0, 8) &gt; $today;,这样您也可以删除 if 语句上方的行。
  • 你帮了很多忙。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-11
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 2020-03-06
  • 2019-07-31
相关资源
最近更新 更多