Question: my problem is a bit similar to some existing topic like [this](https://stackoverflow.com/questions/1028668/get-first-key-in-a-possibly-associative-array)
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"

Answer: 
You can use [`array_keys`](http://php.net/manual/en/function.array-keys.php) 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
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];

```