sam
sam

Reputation: 10084

mixed arrays (text and strings) in a php function

Im trying to get to grips with php (rather unsuccessfully). I keep getting tripped up on the syntax for defining and calling functions

the code ive written is

$alpha = array(1, "alpha");
$beta = array(2, "beta");

function find_best_provider($provider1, $provider2){
    if($provider1 > $provider2){
        return array($provider1[0], $provider1[1]);
    }
    else {
        return array($provider2[0], $provider2[1]);
    }
}

$winner = find_best_provider($alpha, $beta);
echo $winner;

But i keep getting this notice - Notice: Array to string conversion in /Applications/MAMP/htdocs/find_best_provier_func.php on line 17 Array

Im aware what the problem is but im not quite sure how to solve it, any helps much appreciated !

Upvotes: 0

Views: 143

Answers (1)

BenM
BenM

Reputation: 53208

If you're trying to evaluate the first element in the array, try this:

function find_best_provider($provider1, $provider2)
{
    if($provider1[0] > $provider2[0])
    {
        return array($provider1[0], $provider1[1]);
    }
    else 
    {
        return array($provider2[0], $provider2[1]);
    }
}

Upvotes: 2

Related Questions