1

I have a function that returns an array but for some things I only need one of the values from that array. Here is the function:

function url_vars() {
    $bank = strtolower($_GET['bank']);
    $bank = str_replace(',', '', $bank);
    $bank = str_replace(' ', '_', $bank);
    $type = $_GET['type'];
    $term = $_GET['term'];
    return array(
        bank => $bank,
        type => $type,
        term => $term,
        term_yrs => $term / 12
    );
}

I tried to target one value from another function with $bank = url_vars()['bank']; but this seems to be incorrect as it is not working. How can I target a single value from this array? What is the correct way to do that?

1
  • 4
    You really should put ' around those array keys in array()... Commented Feb 23, 2015 at 21:02

2 Answers 2

3

In PHP 5.5 and higher, the url_vars()['bank'] syntax should work. However in lower version, you'll just have to assign the function return to a variable and then access the element from that.

$array = url_vars();
$bank = $array["bank"];
Sign up to request clarification or add additional context in comments.

Comments

2
$vars = url_vars();
$bank = $vars['bank'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.