php search array key and get value
php
Solution
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
$result = $array[$key] ?? null;
Problem
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