peter
peter

Reputation: 11

How to call a php script based on values in an array

I have a set of variables that have randomly assigned values.

E.g.,

$a = 1;
$b = -1;
$c = 2;

And I have a script that corresponds to each variable.

E.g.,

a.php for $a
b.php for $b
c.php for $c

I want to run the script that corresponds with the variable that has the greatest value. So, in this case, I want to run c.php because $c has the greatest value among the three variables.

Thank you so much, Peter

Edit: This is what I have. I've basically put the variables in an array and sorted them from highest to lowest according to the value. But this doesn't solve my problem. I want to run a corresponding php file.

$var = array( 'a' => $a, 'b' => $b, 'c' => $c);
arsort($var);

Upvotes: 0

Views: 55

Answers (1)

Marcio Mazzucato
Marcio Mazzucato

Reputation: 9305

You can get the greatest value using:

$a = 1;
$b = -1;
$c = 2;

$files = array(
    $a => 'a.php',
    $b => 'b.php',
    $c => 'c.php'
);

ksort($files);

echo 'Correct file: ' . array_pop($files);

References:

ksort()

array_pop()

Upvotes: 1

Related Questions