从嵌套在for循环中的if条件返回true的函数

时间:2019-08-29 11:55:13

标签: php function boolean

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

任何帮助或此代码有什么问题,将不胜感激。 预先感谢。

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"
}

1 个答案:

答案 0 :(得分: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;
}