如何从对应的数组值中获取数组键? [英] How to get array key from corresponding array value?
本文介绍了如何从对应的数组值中获取数组键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您可以通过其键轻松获得数组值,如下所示:$value = array[$key]
但是,如果我有该值并且想要它的键,该怎么办.最好的方法是什么?
You can easily get an array value by its key like so: $value = array[$key]
but what if I have the value and I want its key. What's the best way to get it?
推荐答案
You could use array_search()
to find the first matching key.
从手册中:
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red', $array); // $key = 1;
这篇关于如何从对应的数组值中获取数组键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文