我正在尝试获取 true 值,但结果始终显示 false ,如何解决此问题?
$allow = array(
"pdf"=>"application/pdf",
"doc"=>"application/msword",
"docx"=>"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"docx"=>"application/zip"
);
if (in_array("application/vnd.openxmlformats-officedocument.wordprocessingml.document",$allow)) {
echo "true";
} else {
echo "false";
}
在这种情况下,我有两个* .docx文件,但mime类型不同,如何允许它们两个???
答案 0 :(得分:2)
问题是数组包含两个具有相同键的项。即,"application/zip"
会覆盖"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
。以下是print_r($allow);
:
Array
(
[pdf] => application/pdf
[doc] => application/msword
[docx] => application/zip
)
因此,您应为application/zip
指定不同的密钥,例如:
$allow = array( "pdf" => "application/pdf",
"doc" => "application/msword",
"docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"zip" => "application/zip"
);
答案 1 :(得分:1)
我可以看到keys
在你的情况下并不重要。
您可以使用indexed array
代替multidimensional array
。
$allow = array("application/pdf",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/zip");
如果你真的想使用多维数组,那么
为这两个哑剧使用不同的键,例如docx1, docx2
。
$allow = array(
"pdf"=>"application/pdf",
"doc"=>"application/msword",
"docx1"=>"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"docx2"=>"application/zip"
);
答案 2 :(得分:1)
这可能会对您有所帮助,请尝试以下解决方案。您可以将docx作为一个键值,并将mime设置为逗号分隔。
$allow = array(
"pdf" => "application/pdf",
"doc" => "application/msword",
"docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/zip",
);
$allownew = array_values($allow);
if (in_array("application/vnd.openxmlformats-officedocument.wordprocessingml.document", $allownew))
{
echo "true";
}
else
{
echo "false";
}
答案 3 :(得分:1)
也许你应该尝试用#34;翻转"数组的版本:
$allow = array(
"application/pdf"=>"pdf",
"application/msword"=>"doc",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"=>"docx",
"application/zip"=>"docx"
);
现在,当然,您需要搜索数组的键,而不是像
这样的值$k="application/vnd.openxmlformats-officedocument.wordprocessingml.document";
If (array_key_exists($k,$array)) echo $array[$k]; // docx
答案 4 :(得分:0)
检查数组
$allow = array(
"pdf"=>"application/pdf",
"doc"=>"application/msword",
"docx"=>"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"docx"=>"application/zip" // this will be overridden by above index.
);
if(in_array("application/vnd.openxmlformats-officedocument.wordprocessingml.document",$allow)){
echo "true";
}else{
echo "false";
}
你的数组有相同的键,docx,所以最后一个键将被旧的重写。
在你的情况下
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
overriden by "application/zip"
修改强>
$allow = [
"pdf"=>"application/pdf",
"doc"=>"application/msword",
"docx"=>[
"docx"=>"application/zip",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"]
];
条件将是。
function in_array_r($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
return true;
}
}
return false;
}
echo in_array_r("application/vnd.openxmlformats-officedocument.wordprocessingml.document", $allow) ? 'true' : 'false';
我参考了link。