PHP if in_array() 如何获取密钥?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11836741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP if in_array() how to get the key as well?
提问by Tom
Struggling with a tiny problem.
为一个小问题而苦苦挣扎。
I have an array:
我有一个数组:
Array
(
[0] =>
[6] => 6
[3] => 5
[2] => 7
)
I am checking if a set value is in the array.
我正在检查数组中是否有设置值。
if(in_array(5, $array)) {
//do something
} else {
// do something else
}
The thing is, when it find the value 5 in array, I really need the key to work with in my "do something".
问题是,当它在数组中找到值 5 时,我真的需要在我的“做某事”中使用的键。
In this case I need to set:
在这种情况下,我需要设置:
$key = 3;
(key from the found value in_array).
(来自找到的值 in_array 的键)。
Any suggestions?
有什么建议?
回答by DaveRandom
array_search()is what you are looking for.
array_search()就是你要找的。
if (false !== $key = array_search(5, $array)) {
//do something
} else {
// do something else
}
回答by Niko
If you only need the key of the first match, use array_search():
如果您只需要第一场比赛的钥匙,请使用array_search():
$key = array_search(5, $array);
if ($key !== false) {
// Found...
}
If you need the keys of all entries that match a specific value, use array_keys():
如果您需要匹配特定值的所有条目的键,请使用array_keys():
$keys = array_keys($array, 5);
if (count($keys) > 0) {
// At least one match...
}
回答by Pevara
You could just use this http://www.php.net/manual/en/function.array-search.php
你可以只使用这个http://www.php.net/manual/en/function.array-search.php
$key = array_search(5, $array)
if ($key !== false) {
...
回答by chris
You can try
你可以试试
if(in_array(5, $array))
{
$key = array_search(5, $array);
echo $key;
}
this way you know it exists, and if it doesn't it doesn't cause notices, warnings, or fatal script errors depending on what your doing with that key there after.
这样你就知道它存在,如果它不存在,它不会导致通知、警告或致命的脚本错误,这取决于你之后用那个键做什么。
回答by ryanbwork
回答by pmiranda
In case anyone need it in array of arrrays. My case was this:
万一有人需要它的数组。我的情况是这样的:
I had an array like this:
我有一个这样的数组:
$myArray =
array:3 [▼
0 => array:3 [▼
0 => 2
1 => 0
2 => "2019-07-21 23:59:59"
]
1 => array:3 [▼
0 => 3
1 => 2
2 => "2019-07-21 23:59:59"
]
2 => array:3 [▼
0 => 1
1 => 1
2 => "2019-07-21 23:59:59"
]
]
And another one like this (an array of objects):
另一个像这样(一组对象):
$Array2 =
Collection {#771 ▼
#items: array:12 [▼
0 => {#1047 ▼
+"id": 2
+"name": "demografico"
+"dict_key": "demographic"
+"component": "Demographic"
+"country_id": null
+"created_at": null
+"updated_at": null
}
1 => {#1041 ?}
2 => {#1040 ?}
etc...
As the OP, I had to "do something" (use values in a html php template, my case Laravel with blade) with the key where some value was in the array. For my code, I had to use this:
作为 OP,我必须使用数组中某个值的键“做某事”(使用 html php 模板中的值,我的案例 Laravel 使用刀片)。对于我的代码,我不得不使用这个:
foreach($Array2 as $key => $item)
if(false !== $key = array_search($item->id, array_column($myArray, 0))
// Note that $key is overwritten
<input type="number" class="form-control" id="{!! $item->id !!}" value="{{ $myArray[$key][1] }}">

