1

I'm having a multidimensional array in PHP and the following code:

<?php

    $weather = array (
        "AVAILABLE"  => array (
            "first" => "We're having a nice day.", 
            "second" => "We're not having a nice day.", 
            "fifth" => "It's raining out there.", 
            "tenth" => "It's gonna be warm today."));

    function getDomain() {
        if (strpos($_SERVER['SERVER_NAME'], '.com') !== FALSE) {
            return "first";
        }
        elseif (strpos($_SERVER['SERVER_NAME'], '.eu') !== FALSE) {
            return "second";
        }
        else {
            die();
        }
    }

    function myWeather($string) {
        $domain = getDomain();
        return $weather[$string][$domain];
    }

    echo myWeather("AVAILABLE");

?>

When I am at web with domain .com, it should echo value of key "AVAILABLE" in domains key ("first") - We're having a nice day.

When I'll be in site with domain .eu, it should write value of key "AVAILABLE", but in another domains key ("second") - We're not having a nice day.

How can I make this work please? Later there will be more keys in array $weather.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Jan Radosta
  • 147
  • 1
  • 10

1 Answers1

2

Need to add array as a parameter to the function myWeather():-

<?php

$weather = array (
                "AVAILABLE"  => array (
                    "first" => "We're having a nice day.", 
                    "second" => "We're not having a nice day.", 
                    "fifth" => "It's raining out there.", 
                    "tenth" => "It's gonna be warm today."
                )
            );

function getDomain() {
    if (strpos($_SERVER['SERVER_NAME'], '.com') !== FALSE) {
        return "first";
    }
    elseif (strpos($_SERVER['SERVER_NAME'], '.eu') !== FALSE) {
        return "second";
    }
    else {
        return "fifth"; // instead of die(); return some value
    }
}

function myWeather($string,$array) {
    $domain = getDomain();
    return $array[$string][$domain];
}

echo myWeather("AVAILABLE",$weather);

?>

Note:- The above needed to make array available inside function scope

Working output:-https://eval.in/841524

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98