Jump to content

How to output a single value from an associative array, given the key


spryce

Recommended Posts

I came up with this but it outputs the value 3 times... (because there are 3 variables)

 

ie mathsmathsmaths

 

How do I fix this or do it the correct way? Thanks

 

$subject = "maths";
$phone = "1235";
$colour = "red";
$all_vars = compact("subject", "phone", "colour");

get_value($subject);

function get_value($variable){
global $all_vars;
foreach($all_vars as $key=>$value){
	if ($key = $variable) {
		echo $key;
	}
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.