user198989
user198989

Reputation: 4665

Echo string which has highest number?

Let's say I have these;

$z1 = substr_count_array($aldi1, "keyword");
$z2 = substr_count_array($aldi2, "keyword");
$z3 = substr_count_array($aldi3, "keyword");

substr_count_array counts the number that how many times our keyword is included in that string. Now I want to echo string that has the highest count number. I want to echo the string which has highest number. How should I do that ?

Upvotes: 0

Views: 248

Answers (3)

SERPRO
SERPRO

Reputation: 10067

Why don't you store the result from the count in an array. If you store each "aldi" in an array you can do this:

$longest = "";
$max = 0;
foreach($aldi as $text)
{
   $count = substr_count_array($text, "keyword");
   if($count > $max)
   {
      $longest = $text;
      $max = $count;
   }
}
echo $longest. " ". $max . " times";

Upvotes: 2

Jigar Tank
Jigar Tank

Reputation: 1784

Use PHP max function : echo max($z1,$z2,$z3);

Upvotes: 0

Logan Serman
Logan Serman

Reputation: 29880

ManseUK, why not post the answer in an actual answer?

Anyway, he's right:

echo max($z1, $z2, $z3);

Upvotes: 0

Related Questions