I was wondering what is the best way to search keys in an array and return it's value. Something like array_search but for keys. Would a loop be the best way?
我想知道在数组中搜索键并返回其值的最佳方法是什么。类似于array_search但是用于键。循环是最好的方法吗?
Array:
阵:
Array([20120425] => 409 [20120426] => 610 [20120427] => 277
[20120428] => 114 [20120429] => 32 [20120430] => 304
[20120501] => 828 [20120502] => 803 [20120503] => 276 [20120504] => 162)
Value I am searching for : 20120504
我正在寻找的价值:20120504
4 个解决方案
#1
54
The key is already the ... ehm ... key
钥匙已经是......呃...钥匙
echo $array[20120504];
If you are unsure, if the key exists, test for it
如果您不确定,如果密钥存在,请测试它
$key = 20120504;
$result = isset($array[$key]) ? $array[$key] : null;
Minor addition:
次要补充:
$result = @$array[$key] ?: null;
One may argue, that @
is bad, but keep it serious: This is more readable and straight forward, isn't?
有人可能会争辩说,@是坏的,但要保持严肃:这更具可读性和直接性,不是吗?
Update: With PHP7 my previous example is possible without the error-silencer
更新:使用PHP7,我可以在没有错误消音器的情况下使用上一个示例
$result = $array[$key] ?? null;
#2
5
<?php
// Checks if key exists (doesn't care about it's value).
// @link http://php.net/manual/en/function.array-key-exists.php
if (array_key_exists(20120504, $search_array)) {
echo $search_array[20120504];
}
// Checks against NULL
// @link http://php.net/manual/en/function.isset.php
if (isset($search_array[20120504])) {
echo $search_array[20120504];
}
// No warning or error if key doesn't exist plus checks for emptiness.
// @link http://php.net/manual/en/function.empty.php
if (!empty($search_array[20120504])) {
echo $search_array[20120504];
}
?>
#3
4
array_search('20120504', array_keys($your_array));
#4
-1
Here is an example straight from PHP.net
这是一个直接来自PHP.net的例子
$a = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
foreach ($a as $k => $v) {
echo "\$a[$k] => $v.\n";
}
in the foreach you can do a comparison of each key to something that you are looking for
在foreach中,您可以将每个键与您正在寻找的内容进行比较
#1
54
The key is already the ... ehm ... key
钥匙已经是......呃...钥匙
echo $array[20120504];
If you are unsure, if the key exists, test for it
如果您不确定,如果密钥存在,请测试它
$key = 20120504;
$result = isset($array[$key]) ? $array[$key] : null;
Minor addition:
次要补充:
$result = @$array[$key] ?: null;
One may argue, that @
is bad, but keep it serious: This is more readable and straight forward, isn't?
有人可能会争辩说,@是坏的,但要保持严肃:这更具可读性和直接性,不是吗?
Update: With PHP7 my previous example is possible without the error-silencer
更新:使用PHP7,我可以在没有错误消音器的情况下使用上一个示例
$result = $array[$key] ?? null;
#2
5
<?php
// Checks if key exists (doesn't care about it's value).
// @link http://php.net/manual/en/function.array-key-exists.php
if (array_key_exists(20120504, $search_array)) {
echo $search_array[20120504];
}
// Checks against NULL
// @link http://php.net/manual/en/function.isset.php
if (isset($search_array[20120504])) {
echo $search_array[20120504];
}
// No warning or error if key doesn't exist plus checks for emptiness.
// @link http://php.net/manual/en/function.empty.php
if (!empty($search_array[20120504])) {
echo $search_array[20120504];
}
?>
#3
4
array_search('20120504', array_keys($your_array));
#4
-1
Here is an example straight from PHP.net
这是一个直接来自PHP.net的例子
$a = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);
foreach ($a as $k => $v) {
echo "\$a[$k] => $v.\n";
}
in the foreach you can do a comparison of each key to something that you are looking for
在foreach中,您可以将每个键与您正在寻找的内容进行比较