gpt4 book ai didi

PHP - 数组中的 Strpos

转载 作者:行者123 更新时间:2023-12-04 05:04:19 27 4
gpt4 key购买 nike

所以目前我有以下两个数组:

Array ( [0] => image/jpeg [1] => image/png [2] => image/psd ) 
Array ( [0] => png [1] => jpeg [2] => jpg [3] => zip [4] => mov )

顶部数组是正在转发的 $_GET 数据,它们是上传的文件类型。第二个数组是允许的文件类型的逗号列表。基本上,我想要实现的是检查数组中的每个项目是否是允许的文件类型,如果不是,则返回错误消息。

我的代码:
$fileTypes = $_GET['data']; // Get the JSON file types

$fileTypes = json_decode($fileTypes); // Decode the data

$allowedTypes = "png,jpeg,jpg,zip,mov"; // List of allowed file types
$allowedTypes = explode(",", $allowedTypes); // Explode the list to an array

// For each file type in the array
foreach ($fileTypes as $fileType) {

foreach($allowedTypes as $allowedType) {

$pos = strpos($fileType, $allowedType); // Check if the allowed type is contained in the file type

if($pos == FALSE) { echo "False"; } else { echo "True"; }

}

现在的问题是,这将返回以下内容:
False, True, False, False, False (Overall will still equate to False, even though it had a true)
True, False, False, False, False (Overall will still equate to False, even though it had a true)
False, False, False, False, False (Overall will equate to false which it should as it has no trues)

现在,正如您所看到的,当状态为真和假时,代码正在运行,但我需要对其进行整体评估。因此,如果每一行都有一个 true in,则整体状态为 true,但是如果其中一行(如最后一行)包含所有 false,则整体条件变为 false。

不知道这是否有意义?

最佳答案

这似乎适用于 Pendo 的建议。

// For each file type in the array
$state = TRUE;

foreach ($fileTypes as $fileType) {

foreach($allowedTypes as $allowedType) {

$pos = strpos($fileType, $allowedType); // Check if the allowed type is contained in the file type

if($pos === FALSE) { $state = FALSE; } else { $state = TRUE; break; }

}

}

echo $state;

关于PHP - 数组中的 Strpos,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15722965/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com