How to get array key from corresponding array value?

前端 未结 5 898
-上瘾入骨i
-上瘾入骨i 2020-12-01 16:12

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?

5条回答
  •  爱一瞬间的悲伤
    2020-12-01 16:42

    No really easy way. Loop through the keys until you find array[$key] == $value

    If you do this often, create a reverse array/hash that maps values back to keys. Keep in mind multiple keys may map to a single value.

提交回复
热议问题