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:
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
The key is already the … ehm … key
If you are unsure, if the key exists, test for it
Minor addition:
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