如何比较数组中文件的扩展名并抛出错误?

How to compare extension of files in an array and throw error?

我关注了标题为 $aSupportedImages:

的文件扩展名数组
Array
(
    [0] => jpeg
    [1] => jpg
    [2] => gif
    [3] => png
)

我还有另一个名为 $values 的数组,如下所示:

Array
(
    [vshare] => Array
        (
            [course_error.png] => Array
                (
                    [0] => https://www.filepicker.io/api/file/Y0n99udSqS6ZJWYeYcUA
                )

            [before_login.png] => Array
                (
                    [0] => https://www.filepicker.io/api/file/19FWbHh1QNGCo2OINxI6
                )

            [Sample_1.docx] => Array
                (
                    [0] => https://www.filepicker.io/api/file/INjMeEhCSjpZSfZJmQUb
                )

        )

)

现在你可以看到数组[vshare]中的每个键都是一个文件名。我想用数组 $aSupportedImages 中存在的扩展名检查每个此类文件的扩展名。如果任何文件的扩展名与数组 $aSupportedImage 中存在的扩展名不同,则循环应该中断并且它应该 return false。

在上述情况下,对于第三个文件,它应该 return false。由于 .docx 不存在于数组中 $aSupportedImages

我应该怎么做?请帮助我。

请尝试 in_array

 foreach($vshareArray as $key => $value){
      if(in_array($key, $aSupportedImages)){
       echo "valid";
    }else{
     echo "not valid";
    }
    }

如果存在扩展名不受支持的文件,这应该会中断

foreach($values['vshare'] as $file)
{
    if(!in_array(pathinfo($file, PATHINFO_EXTENSION), $aSupportedImages))
        break;
}

试试这个:

<?php
      
  function get_ext($filename) {
  
    return strtolower(substr($filename, strrpos($filename, '.')));

  }

  $ext_authorized = array('.jpg', '.jpeg', '.png', '.gig');

  $values = array (
    'vshare' => array (
      'course_error.png' => array ('https://www.filepicker.io/api/file/Y0n99udSqS6ZJWYeYcUA'),
      'before_login.png' => array ('https://www.filepicker.io/api/file/19FWbHh1QNGCo2OINxI6'),
      'Sample_1.docx' => array ('https://www.filepicker.io/api/file/INjMeEhCSjpZSfZJmQUb')
    )
  );

  foreach($values['vshare'] as $key => $val) {
      
    if (in_array(get_ext($key), $ext_authorized)) {
  
      //do something

    } else {

      //do something

    }
    
  }
?>