简体   繁体   中英

Get single value from function returned array in PHP

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?

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"];
$vars = url_vars();
$bank = $vars['bank'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM