Dirty Bird Design
Dirty Bird Design

Reputation: 5553

Access specific key in foreach array

I need to access a specific custom header to determine what content to serve. I can get the headers and output an array like this:

<?php
    headers = apache_request_headers();
    foreach ($headers as $header => $value) {
        echo "$header: $value <br />\n";
     }
?>

It outputs all headers, the one I need to access is: X-Language-Locale: it-IT

I need to parse all of the array for "X-Language-Locale" and run an if else statement to determine what content to serve. How do I do this?

Upvotes: 0

Views: 58

Answers (2)

tripathiakshit
tripathiakshit

Reputation: 644

You can probably use indexed access, since you have a constant key that you want to look for. Most objects that have key-value pairs can also be accessed using the key as an index.

<?php
    $headers = apache_request_headers();
    $lang_locale = $headers["X-language-locale"];
    if ($lang_locale == "it-IT") {
        // DO SOMETHING
    } else {
        // DO SOMETHING ELSE
    }
?>

Upvotes: 1

nagyl
nagyl

Reputation: 1634

You can access it without the foreach loop.

if($headers['X-Language-Locale'] == 'it-IT') {
    echo 'ok';
}else {
    echo 'not italian';
}

Upvotes: 1

Related Questions