使用 max 函数从多维数组中获取最大值


Getting Max Value From Multi Dimensional Array Using max Function

<?php
$heading = "The Flash 3";
$arrays = array("the-flash","tv-shows","games","videos");
$playerdata = array();
foreach ($arrays as $key => $tag) {
similar_text($tag, $heading, $percent); 
$playerdata = array(
'tag' => $tag,
'goals' => $percent
 );   
 $allplayerdata[] = $playerdata;
 }
 print_r($allplayerdata);
 ?>
Result Of print_r($allplayerdata);
([0] => Array ( [tag] => the-flash [goals] => 60 ) 
[1] => Array ([tag] => tv-shows [goals] => 21.052631578947 ) 
[2] => Array ( [tag] => games [goals] => 25 ) 
[3] => Array ( [tag] => videos [goals] => 23.529411764706 ) )    

我所需要的只是标签的最大值和名称,即闪光灯和目标 = 60值来自数据库。谢谢

你可以

用这个替换你的foreach循环:

$max = $max_tag = NULL;
foreach ($arrays as $tag) {
    similar_text($tag, $heading, $percent); 
    $playerdata = array(
        'tag' => $tag,
        'goals' => $percent
    );   
    $allplayerdata[] = $playerdata;
    if (is_null($max) || $percent > $max) {
        $max = $percent;
        $max_tag = $tag;
    }
}
echo "Max = {$max}, max tag = {$max_tag}";