Return 键的索引名称,如果它是 PHP 中的保留字

Return the index name of key if it is reserved word in PHP

我的问题有点类似于一些现有的话题,比如 this 但解决方案不适合我的问题。 我有一个函数数组,我想 return 索引 :

protected function commonUploadErrors($key)
    {
         $uploadErrors = array(
                UPLOAD_ERR_INI_SIZE     => "File is larger than the specified amount set by the server",
                UPLOAD_ERR_FORM_SIZE    => "File is larger than the specified amount specified by browser",
                UPLOAD_ERR_PARTIAL      => "File could not be fully uploaded. Please try again later",
                UPLOAD_ERR_NO_FILE      => "File is not found",
                UPLOAD_ERR_NO_TMP_DIR   => "Can't write to disk, due to server configuration ( No tmp dir found )",
                UPLOAD_ERR_CANT_WRITE   => "Failed to write file to disk. Please check you file permissions",
                UPLOAD_ERR_EXTENSION    => "A PHP extension has halted this file upload process"
            );

            return $uploadErrors[$key];
        }

如果key = 3我想returnUPLOAD_ERR_NO_FILE而不是"File is not found", 我尝试了 return key($uploadErrors[$key]); 和 return index_keys$uploadErrors[$key]; 但没有用

我用过

$result = array_keys($uploadErrors);
     return $result[$key];

和我return“3”一样,我认为它是保留错误代码UPLOAD_ERR_NO_FILE的真实值。所以我想 return 代码的名称而不是它的值,当我回显 UPLOAD_ERR_FORM_SIZE 时,我得到 "2"

尝试使用 array_keys

$newArray = array_keys($uploadErrors);
return $uploadErrors[$newArray[$key]];

您可以使用PHP的array_keys函数,其中return数组的所有键或键的子集

$uploadErrors = array(
    'UPLOAD_ERR_INI_SIZE' => "File is larger than the specified amount set by the server",
    'UPLOAD_ERR_FORM_SIZE' => "File is larger than the specified amount specified by browser",
    'UPLOAD_ERR_PARTIAL' => "File could not be fully uploaded. Please try again later",
    'UPLOAD_ERR_NO_FILE' => "File is not found",
    'UPLOAD_ERR_NO_TMP_DIR' => "Can't write to disk, due to server configuration ( No tmp dir found )",
    'UPLOAD_ERR_CANT_WRITE' => "Failed to write file to disk. Please check you file permissions",
    'UPLOAD_ERR_EXTENSION' => "A PHP extension has halted this file upload process"
);
$key = 3;
$newArray = array_keys($uploadErrors);
echo $newArray[$key];//UPLOAD_ERR_NO_FILE

所以在这里 $newArray 将得到像

这样的键数组
Array
(
    [0] => UPLOAD_ERR_INI_SIZE
    [1] => UPLOAD_ERR_FORM_SIZE
    [2] => UPLOAD_ERR_PARTIAL
    [3] => UPLOAD_ERR_NO_FILE
    [4] => UPLOAD_ERR_NO_TMP_DIR
    [5] => UPLOAD_ERR_CANT_WRITE
    [6] => UPLOAD_ERR_EXTENSION
)

并且仅在 $newArray 变量中传递键作为 echo $newArray[$key]; 将回显相应键的值

尝试以下操作:

var_dump(array_slice($input, 3, 1));

然后您必须首先将您的密钥放入引号中。 然后将你的数组键提取到一个数组中,我称之为错误 然后让你的新数组键成为 $key 变量

    $uploadErrors = array(
                'UPLOAD_ERR_INI_SIZE'     => "File is larger than the specified amount set by the server",
                'UPLOAD_ERR_FORM_SIZE'    => "File is larger than the specified amount specified by browser",
                'UPLOAD_ERR_PARTIAL'      => "File could not be fully uploaded. Please try again later",
                'UPLOAD_ERR_NO_FILE'     => "File is not found",
                'UPLOAD_ERR_NO_TMP_DIR'   => "Can't write to disk, due to server configuration ( No tmp dir found )",
                'UPLOAD_ERR_CANT_WRITE'   => "Failed to write file to disk. Please check you file permissions",
                'UPLOAD_ERR_EXTENSION'    => "A PHP extension has halted this file upload process"
            );

$errors = array_keys ($uploadErrors);

return $errors[$key];

您的 $uploadErrors 包含键的常量,因此它不会将常量的名称作为键,而是将它们的值 as indicated in the PHP Manual,例如

$uploadErrors = [
    UPLOAD_ERR_INI_SIZE => "foo", 
    UPLOAD_ERR_FORM_SIZE => "bar", 
    …
]

将得到这个数组:

[1 => "foo", 2 => "bar", …]

由于 UPLOAD_ERR_ 常量是核心常量,您可以通过编程方式获取它们,然后将它们的值映射到名称,例如像这样:

foreach (get_defined_constants(true)['Core'] as $name => $value) {
    if (strpos($name, 'UPLOAD_ERR_') === 0) {
        $uploadErrorMap[$value] = $name;
    }
}

echo $uploadErrorMap[array_keys($uploadErrors)[3]];

然后将 "UPLOAD_ERR_NO_FILE" 作为字符串输出。

查看此演示:https://eval.in/367710

如果您希望 $uploadErrors 包含实际常量 names,请将它们放在引号中,.


参考: