# Checking for Keys and Looking Up Values in Arrays, Restricting Files in .htaccess

- Link: https://cagrimmett.com/2016/02/24/arrays/
- Published: 2016-02-24T05:00:00-05:00
- Author: Chuck Grimmett

### Today I learned:

#### Checking for a key in an array

    ```highlight
     key_exists($key, $array) 
    ```

For the Toggl Slack slash command, I’m using it to check if someone’s Toggl API 
key is in the array in `variables.php`:

    ```highlight
     if (key_exists($user_name, $toggl_api_key_mapping) == FALSE) { 	echo ":warning: You haven't set up your Toggl API key with this command yet. Get it at the bottom of https://toggl.com/app/profile and send it  to @$your_admin_name."; } 
    ```

Notes:

 * Case sensitive. If you need it to be insensitive, use `strtolower()` on the `
   $key`
 * Boolean (returns TRUE or FALSE)

---

#### Looking up a key and returning a value in an array

    ```highlight
     $array['key'] 
    ```

For the Toggl Slack slash command, I’m using it to set someone’s API key from the
array in `variables.php`:

    ```highlight
     $toggl_api_key = $toggl_api_key_mapping[$user_name]; 
    ```

---

#### Blocking access to files via .htaccess

If you are serving something on your webserver that you don’t want anyone else to
be able to access, you can restrict access to by adding this snippet to your site’s`.
htaccess` file:

    ```highlight
     log.csv> 	order allow,deny 	deny from all  
    ```

If you need only a certain IP address, you can achieve this by adding `allow from
0.0.0.0` (replace that number with your IP address):

    ```highlight
     log.csv> 	order allow,deny 	allow from 0.0.0.0 	deny from all  
    ```
