【问题标题】:Use PHP Associative Array in If Condition在 If 条件下使用 PHP 关联数组
【发布时间】:2020-04-20 08:08:57
【问题描述】:

我对 if 条件中语句的含义及其用途有疑问。我在下面发布了完整的 PHP 代码。这是我目前正在研究的另一个人的代码,我的问题是关于特定的代码行(请参阅我的问题的下一段)。

<?php
    $dir = '../assets/videos/';             
    if(is_dir($dir)) {
        $filesread = array();
        $dh = opendir($dir);
        print_r($filesread);
        while(($file = readdir($dh)) !== false) {
            if($file != '.' && $file != '..') {
                //returns all file names wihout extension
                $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);    
                if($filesread[$withoutExt] !== true) {                                      
                    $hasSQL = false;                                        
                    foreach($films as $f) {                                         
                        if($f['file_name'] == $withoutExt) {                                            
                            echo '
                                <div class="row">
                                    <div class="col-xs-8">
                                        <a href="'.$dir.$f['file_name'].'.mp4" target="_blank">'.$withoutExt.'</a>
                                    </div>
                                    <div class="col-xs-4">
                                        [<a class="edit" id="'.$f['filmID'].'" data-title="'.$f['title'].'" data-category="'.$f['categoryID'].'" data-file-name="'.$f['file_name'].'" data-desc="'.$f['description'].'">Edit</a>]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<a href="films.php?remove='.$f['filmID'].'">Remove</a>]
                                    </div>
                                </div><br />';
                            $hasSQL = true;
                            print_r($withoutExt);
                        }
                    }
                    if($hasSQL == false) { 
                        echo '
                            <div class="row">
                                <div class="col-xs-8">
                                    <a href="'.$dir.$withoutExt.'.jpg" target="_blank">'.$withoutExt.'</a>: <strong>DOES NOT HAVE DATA</strong>
                                </div>
                                <div class="col-xs-4">
                                    [<a class="add" data-file-name="'.$withoutExt.'"">Add Data</a>]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<a href="films.php?remove='.urlencode($withoutExt).'">Remove</a>]
                                </div>
                            </div><br />';
                    }
                    $filesread[$withoutExt] = true;
                }   
            }
        }                       
    } else {
        echo 'Invalid directory';                                   
    }
?>

在这个 php 代码中,我不太确定 if($filesread[$withoutExt] !== true) 语句是什么意思。

  if($filesread[$withoutExt] !== true) {//code to be excute}

我不知道$filesread[$withoutExt] 是什么。我所知道的是$filesread = array();$withoutExt 是没有扩展名的文件名。我的猜测是程序员试图使用$withoutExt 作为从$filesread 数组中获取值的键,但$filesread 是一个空数组。他如何从空数组中获取值?有谁了解$filesread[$withoutExt] 的含义,以及这个 if 语句的目的?非常感谢!

【问题讨论】:

    标签: php arrays file directory conditional-statements


    【解决方案1】:

    $filesread 是一个数组,其中包含没有扩展名的文件名作为键。对于一个假设已经列出的项目,其标志为 true。赋值发生在输出之后:

    $filesread[$withoutExt] = true;
    

    我的猜测是可能有多个文件名包含不同的扩展名。也许每部电影的字幕或缩略图文件。而且您不想多次列出它们。

    因此检查:

    $filesread[$withoutExt] !== true
    

    对于没有扩展键的数组,哪个读取的值不为真。

    然而,这里的表达式可以很容易地写成:

    !isset($filesread[$withoutExt])
    

    但由于这是代码的摘录,因此该数组可能包含在其他地方分配的其他值。

    在第一次运行时,数组键将不存在,因此在检查值为 true 时可能会触发警告,因此您应该换成更明确的:

    !isset($filesread[$withoutExt]) && ($filesread[$withoutExt] !== true)
    

    【讨论】:

    • 您对 if 语句的假设是正确的!该目录具有多个具有不同扩展名的相同文件名。如film.mp4和film.jpg。还有一个问题,我打印了$filesread 并得到了这个:Array([filmname1]&gt;1,[filmname2]&gt;1,[filmname3]&gt;1);正如你所说,$filesread 是一个包含文件名的数组,没有扩展名作为键,但是$filesread 如何获取所有文件名作为它的键(我知道 $filesread 第一次是一个空数组)?代码的哪一部分做了这个分配?另外,为什么 `$filesread` 中的所有值都是 1?谢谢!
    • $filesread[$withoutExt] = true; 是分配发生的地方。 $withoutExt 是 preg_replace 的结果。
    • 您的意思是$filesread[$withoutExt] = true;$withoutExt 指定为$filesread 数组的键吗?另外,你能告诉我为什么$filesread数组中的所有值都是1。基于$filesread[$withoutExt] = true;,值不应该是真的吗?
    • 是的,将true 指定为$filesread 中键$withoutExt 的值。您可能会从数组上的 print_r 中看到值 1。试试var_dump,它更能揭示其中存储的类型和值。
    猜你喜欢
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2017-03-18
    • 2019-03-05
    相关资源
    最近更新 更多