Posts Tagged ‘ key ’

Accessing Array Values with a variable key

From last few days I have been trying to figure out how to access an array value in PHP using a key value that is stored in a string. So finally i was able to frame a function that could just do that.
Following is the code to get a value from an array with known key
`$arr = Array with values
$key = string with keys seperated with –

function get_array_val($arr,$key)
	$e= explode('-',$key);
	$ar = &$arr;
	foreach($e as $v)
		$ar = $ar[$v];
	return $arr;

Continue reading