如果key在变量中,PHP如何从数组中获取值

时间:2021-06-29 22:07:43

I have a key stored in a variable like so:

我有一个密钥存储在一个变量中,如下所示:

$key = 4;

I tried to get the relevant value like so:

我试着像这样得到相关的值:

$value = $array[$key];

but it failed. Help.

但它失败了。帮帮我。

4 个解决方案

#1


22  

Your code seems to be fine, make sure that key you specify really exists in the array or such key has a value in your array eg:

您的代码似乎没问题,请确保您指定的密钥确实存在于数组中,或者此类密钥在数组中具有值,例如:

$array = array(4 => 'Hello There');
print_r(array_keys($array));
// or better
print_r($array);

Output:

输出:

Array
(
    [0] => 4
)

Now:

现在:

$key = 4;
$value = $array[$key];
print $value;

Output:

输出:

Hello There

#2


5  

$value = ( array_key_exists($key, $array) && !empty($array[$key]) ) 
         ? $array[$key] 
         : 'non-existant or empty value key';

#3


2  

As others stated, it's likely failing because the requested key doesn't exist in the array. I have a helper function here that takes the array, the suspected key, as well as a default return in the event the key does not exist.

正如其他人所说,它可能会失败,因为数组中不存在请求的密钥。我在这里有一个辅助函数,它接受数组,可疑密钥,以及在密钥不存在的情况下的默认返回。

    protected function _getArrayValue($array, $key, $default = null)
    {
        if (isset($array[$key])) return $array[$key];
        return $default;
    }

hope it helps.

希望能帮助到你。

#4


0  

It should work the way you intended.

它应该按照你的意图工作。

$array = array('value-0', 'value-1', 'value-2', 'value-3', 'value-4', 'value-5' /* … */);
$key = 4;
$value = $array[$key];
echo $value; // value-4

But maybe there is no element with the key 4. If you want to get the fiveth item no matter what key it has, you can use array_slice:

但也许没有键4的元素。如果你想获得第五个项目,无论它有什么键,你可以使用array_slice:

$value = array_slice($array, 4, 1);

#1


22  

Your code seems to be fine, make sure that key you specify really exists in the array or such key has a value in your array eg:

您的代码似乎没问题,请确保您指定的密钥确实存在于数组中,或者此类密钥在数组中具有值,例如:

$array = array(4 => 'Hello There');
print_r(array_keys($array));
// or better
print_r($array);

Output:

输出:

Array
(
    [0] => 4
)

Now:

现在:

$key = 4;
$value = $array[$key];
print $value;

Output:

输出:

Hello There

#2


5  

$value = ( array_key_exists($key, $array) && !empty($array[$key]) ) 
         ? $array[$key] 
         : 'non-existant or empty value key';

#3


2  

As others stated, it's likely failing because the requested key doesn't exist in the array. I have a helper function here that takes the array, the suspected key, as well as a default return in the event the key does not exist.

正如其他人所说,它可能会失败,因为数组中不存在请求的密钥。我在这里有一个辅助函数,它接受数组,可疑密钥,以及在密钥不存在的情况下的默认返回。

    protected function _getArrayValue($array, $key, $default = null)
    {
        if (isset($array[$key])) return $array[$key];
        return $default;
    }

hope it helps.

希望能帮助到你。

#4


0  

It should work the way you intended.

它应该按照你的意图工作。

$array = array('value-0', 'value-1', 'value-2', 'value-3', 'value-4', 'value-5' /* … */);
$key = 4;
$value = $array[$key];
echo $value; // value-4

But maybe there is no element with the key 4. If you want to get the fiveth item no matter what key it has, you can use array_slice:

但也许没有键4的元素。如果你想获得第五个项目,无论它有什么键,你可以使用array_slice:

$value = array_slice($array, 4, 1);