Get value in a 2D array in PHP -
i have array:
$array = array ( "key1" => "value 1", "key2" => "value 2", "key3" => "value 3", "key4" => "value 4", ... );
i submitting key form, want assign variable value belongs key, example:
$key = $_post['key']; $value = ?????; //this need
this might simple, haven't done in long time , have forgotten how it.
thanks.
this should trick:
$value = $array[$key];
or without variable:
$value = $array[$_post['key']];
if receive notice: undefined index ...
should check array before try value with:
$value = array_key_exists($_post['key'], $array) ? $array[$_post['key']] : null;
this condition checks if key exists. if so, gets value. if not, value of $value
null
.
Comments
Post a Comment