0

my problem is a bit similar to some existing topic like this but the solution didn't fit to my problem. I have an array in function, I want to return the index :

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];
        }

If key = 3 I want to return UPLOAD_ERR_NO_FILE and not "File is not found", I tried return key($uploadErrors[$key]); and return index_keys$uploadErrors[$key]; but didn't work

I used

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

and I return "3" as well, I think it is the real value of the reserved error code UPLOAD_ERR_NO_FILE. So I want to return the name of the code not its value, when I echo UPLOAD_ERR_FORM_SIZE , I get "2"

Community
  • 1
  • 1
  • 1
    you mean if index is 3, you want to return the key? – trainoasis May 20 '15 at 11:48
  • no, I mean what I wrote, I just corrected and added the function header –  May 20 '15 at 11:49
  • 1
    Unless you create a reverse map, you'll never get `UPLOAD_ERR_INI_SIZE` back from anything. Only the *constant's value* will be returned if anything, not the *constant name*. – deceze May 20 '15 at 12:01

5 Answers5

0

Try use array_keys

$newArray = array_keys($uploadErrors);
return $uploadErrors[$newArray[$key]];
Gordon
  • 312,688
  • 75
  • 539
  • 559
Andrii Filenko
  • 954
  • 7
  • 17
0

You can use array_keys function of PHP, which return all the keys or a subset of the keys of an array

$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

So over here $newArray will get the array of keys like as

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
)

and just passing the key within $newArray variable as echo $newArray[$key]; will echo the value for that respective key

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
  • I got "3", I think the code UPLOAD_ERR_NO_FILE equal 3 , see this (http://php.net/manual/fr/features.file-upload.errors.php) so I think the correct is correct but I need to return the name of the error code not its real value –  May 20 '15 at 11:59
0

Then you must first place your keys into quotes. Then extract your array keys into an array, which i called error Then let your new array keys be the $key variable

    $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];
Gideon Appoh
  • 678
  • 1
  • 6
  • 15
0

Your $uploadErrors contains constants for the keys, so it will not have the name of the constants as keys, but their values as indicated in the PHP Manual, e.g.

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

will result in this array:

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

Since the UPLOAD_ERR_ constants are Core constants, you can get them programmatically and then map their values to the names, e.g. something like this:

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

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

This will then output "UPLOAD_ERR_NO_FILE" as a string.

See this demo: https://eval.in/367710

If you want your $uploadErrors to contain the actual constant names, put them into quotes, as shown here.


Reference:

Community
  • 1
  • 1
Gordon
  • 312,688
  • 75
  • 539
  • 559
-1

Try the following:

var_dump(array_slice($input, 3, 1));
kenorb
  • 155,785
  • 88
  • 678
  • 743
Pyton
  • 1,291
  • 8
  • 19
  • this will return the value but the OP asked for the key. Quoting *If key = 3 I want to return UPLOAD_ERR_NO_FILE and not "File is not found"* – Gordon May 20 '15 at 11:54