spryce Posted April 30, 2011 Share Posted April 30, 2011 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; } } } Link to comment https://forums.phpfreaks.com/topic/235161-how-to-output-a-single-value-from-an-associative-array-given-the-key/ Share on other sites More sharing options...
spryce Posted April 30, 2011 Author Share Posted April 30, 2011 talk about doing things the hard way.... echo $all_vars['subject']; Link to comment https://forums.phpfreaks.com/topic/235161-how-to-output-a-single-value-from-an-associative-array-given-the-key/#findComment-1208556 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.