0

I'm trying to isolate text that I've appended to variables that only contain numbers. I've looked it up and I can't find anything that works or is even close to what I'm trying to do. Here's what I'm looking for:

$winning = max(array($var1."Var_One", $var2."Var_Two", $var3."Var_Tree", $var4."Var_Four"));
$winning = {the function that I'm missing}

I want to find the string that accompanies the highest value variable and then use it in a switch case, for example:

switch ($winning) {
    case "Var_One":
    echo "This variable is the highest value.";
    break;
    case "Var_Two":
    echo "This variable is the highest value.";
    break;
    case "Var_Three":
    echo "This variable is the highest value.";
    break;
    default:
    echo "Values are tied.";
}

I've tried using a regex solution which didn't work and strpos does NOT do what I need.

Any help is appreciated.

ANSWER:

function getMaxValue(array $list) {
    $result = [];
    foreach($list as $k => $v) {
        if(!isset($result["value"]) || $result["value"] < $v) {
            $result = [
                "value" => $v,
                "key" => $k,
            ];
        }
    }
    return $result;
}
$winning = ["Var_One" => $var1, "Var_Two" => $var2, "Var_Three" => $var3];
$winning = getMaxValue($winning);
if(strpos($winning,'Var_One') !== false) {
    echo "Var_One is currently winning by ".$var1."!";
} else if(strpos($winning,'Var_Two') !== false) {
    echo "Var_Two is currently winning by ".$var2."!";
} else if(strpos($winning,'Var_Three') !== false) {
   echo "Var_Three is currently winning by ".$var3."!";
} else {
    echo "Tie.";
}
4
  • So the function you are looking for must return the maximum value of the array based on the $varXvariables?
    – ojovirtual
    Nov 4, 2014 at 12:19
  • 1
    I'm not entirely sure WHY you are doing this. Why not put the text in the array keys and the variables in the value? You'd probably avoid this whole problem then.
    – Erik
    Nov 4, 2014 at 12:25
  • Yes. The max() is working fine for me but I can't seem to isolate the string that I appended to the varX variables. Nov 4, 2014 at 12:25
  • The result of using keys and values is that it returns the value of the variables instead of the name of the key. Nov 4, 2014 at 12:39

2 Answers 2

0

I think you might be looking for something like this:

function getMaxValue(array $list)
{
    $result = [];
    foreach ($list as $k => $v) {
        if (!isset($result["value"]) || $result["value"] < $v) {
            $result = [
                "value" => $v,
                "key" => $k,
            ];
        }
    }
    return $result;
}

Test:

$list = ["one" => 2, "two" => 0, "three" => 5, "four" => 4];
var_dump(getMaxValue($list));

Result:

array(2) {
  ["value"]=>
  int(5)
  ["key"]=>
  string(5) "three"
}
1
  • After adapting my script a little I made this work, thanks for your help. Nov 4, 2014 at 13:30
0
$winning = max(array_map("intval", array($var1."Var_One", $var2."Var_Two", $var3."Var_Tree", $var4."Var_Four")));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.