在不知道一对关联数组中的键的情况下获取值 [英] Get value without knowing key in one-pair-associative-array
问题描述
有一个关联数组,其中只有一对 key=>value
.
There is an associative array with only one pair key=>value
.
我不知道它是关键,但我需要得到它的价值:
I don't know it's key, but I need to get it's value:
$array = array('???' => 'value');
$value = // ??
$array[0]
不起作用.
我怎样才能得到它的价值?
How can I get it's value?
推荐答案
由于数组中只有一个元素,因此您也可以执行以下任一函数来获取值.
You can also do either of the following functions to get the value since there's only one element in the array.
$value = reset( $array);
$value = current( $array);
$value = end( $array);
另外,如果你想使用array_keys()
,你需要这样做:
Also, if you want to use array_keys()
, you'd need to do:
$keys = array_keys( $array);
echo $array[ $keys[0] ];
获取值.
作为更多选项,您还可以使用 array_pop()
或 array_shift()
来获取值:
As some more options, you can ALSO use array_pop()
or array_shift()
to get the value:
$value = array_pop( $array);
$value = array_shift( $array);
最后可以使用array_values()
获取数组的所有值,然后取第一个:
Finally, you can use array_values()
to get all the values of the array, then take the first:
$values = array_values( $array);
echo $values[0];
<小时>
当然,还有很多其他的选择;一些愚蠢的,一些有用的.
Of course, there are lots of other alternatives; some silly, some useful.
$value = pos($array);
$value = implode('', $array);
$value = current(array_slice($array, 0, 1));
$value = current(array_splice($array, 0, 1));
$value = vsprintf('%s', $array);
foreach($array as $value);
list(,$value) = each($array);
这篇关于在不知道一对关联数组中的键的情况下获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!