回显字符串中数字最高的字符串


Echo string which has highest number?

假设我有这些;

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

substr_count_array计算关键字在该字符串中包含的次数。现在我想回显具有最高计数数的字符串。我想回显数字最高的字符串。我该怎么做呢?

为什么不将计数的结果存储在数组中呢?如果您将每个"aldi"存储在一个数组中,您可以这样做:

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

ManseUK,为什么不在一个实际的答案中发布答案呢?

无论如何,他是对的:

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

使用PHP max函数:echo max($z1,$z2,$z3);