Reputation: 5309
I have
$max = max($a, $b, $c);
which results, lets say 30. I don't know which variable($a or $b or $c
) gives the result 30.
From this result(30) i have to find the variable name
.
The result should be $a or $b or $c
.How can I find this??
NOTE: Find the max is only a sample case.I need the variable name from a value from a group of variables.
Upvotes: 1
Views: 400
Reputation: 1457
I am not asking you why? Here is the solution that I think will work
use get_defined_vars() function which will return all the defined variables in an array. Then check the value of all the variable names with the result value (of which you want to know the variable).
Upvotes: -1
Reputation: 1053
$a = 30; $b=20; $c=40;
$arr[$a] = '$a'; $arr[$b] = '$b'; $arr[$c] = '$c';
echo 'max val='.$max = max($a, $b, $c);
echo 'var name='.$arr[$max];
Upvotes: 0
Reputation: 332
$array = array('a'=> $a, 'b'=> $b, 'c'=> $c);
$maxs = array_keys($array, max($array));
Upvotes: 3
Reputation: 6824
$max = max($a, $b, $c);
// Create an array so we can find the name for each variable we're checking
$var_array = array(
'$a' => $a,
'$b' => $b,
'$c' => $c,
);
// Use the array search function to find our key: http://php.net/array_search
$max_var_name = array_search($max, $var_array, true);
Upvotes: 3