Key value from array

Hello everyone, I am having an issue with a pretty simple PHP code i was wondering if someone could help me fix, i know it’s wrong.
I have to use an associative array already made named $salary[] which contains the salaries of the employees and return the salary of the employee named in the parameter

function getValueByKey($parameter1) {
$salary=$salaries["$parameter1"];

};

I know i don’t have anything returned, the directions say this should be completed in one line of code so I dont understand how that adds up.

Here is a similar function that I wrote last week on a project. See if you can understand how it works.

[php]function getRegion( $r )
{
$regionNames = array(
// “jax” => array(“long” => “jacksonville—river-city-marketplace”,
// “lat” => “jacksonville—st-johns-town-center”),
“car” => “tampa-carrollwood”,
“nmi” => “north-miami”,
“njx” => “jacksonville-–-river-city-marketplace”,
“hia” => “hialeah”,
“pnp” => “pinellas-park”,
“ftl” => “fort-lauderdale”,
“mia” => “miami”,
“pen” => “pensacola”,
“ftm” => “fort-myers”,
“orl” => “orlando”,
“sar” => “sarasota”,
“jax” => “jacksonville-–-st-johns-town-center”,
“pbc” => “palm-beach”,
“plk” => “winter-haven”,
“tam” => “tampa-west-shore-blvd”,
“tal” => “tallahassee”,

    );
    
    if ( in_array( $r, array_keys( $regionNames ) ))
    {
        return $regionNames[$r];
    } else {
        return false;
    }

}[/php]

Ignore the first value, that was testing pulling from a multidimensional array.

Probably done pretty simply:
[php]
function getSalary($employee) {
//Your salary array
$salary = array(‘bob’ => 50000);
return array_key_exists($employee, $salary) ? $salary[$employee] : false;
}
[/php]
If $salary is outside the function you might want to pass it in as well
[php]
function getSalary($employee, $salary = array()) {
return array_key_exists($employee, $salary) ? $salary[$employee] : false;
}
[/php]

When printing just check the value is not false and print nothing
[php]
echo getSalary($employee, $salary) !== false ? getSalary($employee, $salary) : ‘’;
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service