简体   繁体   English

从PHP中的函数返回数组中获取单个值

[英]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']; 我试图用$bank = url_vars()['bank'];定位另一个函数的一个值$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. 在PHP 5.5及更高版本中, url_vars()['bank']语法应该起作用。 However in lower version, you'll just have to assign the function return to a variable and then access the element from that. 但是,在较低版本中,您只需要将函数return分配给变量,然后从中访问元素。

$array = url_vars();
$bank = $array["bank"];
$vars = url_vars();
$bank = $vars['bank'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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