【问题标题】:Sorting array value in descending order按降序对数组值进行排序
【发布时间】:2018-07-26 20:57:36
【问题描述】:

如何按降序对值进行排序?我试过 arsort();但在我的情况下它不起作用:

$text1 = 'Android SDK, application, familiar with MVP architecture, android studio, angular, angular js';

$skill = array("android sdk"=>"3", "application"=>"1", "angular"=>"2", "android studio"=>"3", "angular js"=>"3");

foreach ($skill as $skills => $weight)            {

if (preg_match_all("~\b$skills\b~i", $text1, $matchWords)) {  

$matchWords = $matchWords[0];

$matchWords = array_unique($matchWords);

}  

$text2 = 'Native Android development';

// Filter $words, keep only the items that are not present in $text2
$missing = array_filter(
    $matchWords,
    function($w) use ($text2) {
        // return TRUE when $w is not in $text
        return preg_match('/\b'.preg_quote($w, '/').'\b/i', $text2) == 0;
});


$weight = array($weight);
$dev = array_combine($missing, $weight);
arsort($dev);

foreach($dev as $x => $x_value) 
     echo "Key: " . $x . " Value: " . $x_value;
     echo "<br>";

}

输出:

键:Android SDK 值:3 关键:应用价值:1 键:角度值:2 键:android studio 值:3 键:angular js 值:3

但我想得到这个结果:

键:Android SDK 值:3 键:android studio 值:3 键:角度 js 值:3 键:角度值:2 键:应用值:1

编辑:

我没有按照建议找到答案here

【问题讨论】:

标签: php arrays sorting


【解决方案1】:

您的数组未正确排序的原因是您试图对单个元素数组进行排序。如果您在代码中添加调试打印,您将看到$dev 在您调用arsort 时只有一个元素。您需要在循环中组装$dev,然后对其进行排序。试试这样的:

$text1 = 'Android SDK, application, familiar with MVP architecture, android studio, angular, angular js';
$text2 = 'Native Android development';
$skill = array("android sdk"=>"3", "application"=>"1", "angular"=>"2", "android studio"=>"3", "angular js"=>"3");

foreach ($skill as $skills => $weight) {
    if (preg_match_all("~\b$skills\b~i", $text1, $matchWords)) {  
        $matchWords = $matchWords[0];
        $matchWords = array_unique($matchWords);
    }  
    // Filter $words, keep only the items that are not present in $text2
    $missing = array_filter(
        $matchWords,
        function($w) use ($text2) {
            // return TRUE when $w is not in $text
            return preg_match('/\b'.preg_quote($w, '/').'\b/i', $text2) == 0;
    });

    if (count($missing)) $dev[$missing[0]] = $weight;
}
arsort($dev);

foreach($dev as $x => $x_value) {
    echo "Key: " . $x . " Value: " . $x_value;
    echo "<br>";
}

输出:

Key: Android SDK Value: 3
Key: android studio Value: 3
Key: angular js Value: 3
Key: angular Value: 2
Key: application Value: 1

您的循环代码可以大大简化,但这是另一个问题...

【讨论】:

  • 谢谢尼克,这对我有用。虽然我对此感到困惑:if (count($missing)) $dev[$missing[0]] = $weight; 我也不太明白为什么来自 ` $d​​ev = array_combine($missing, $weight); 的数组`没用。会研究这个。任何简化循环的提示将不胜感激。
  • 嗨,Seb,我认为如果您将调试打印放入循环中,您会发现很多代码并没有做太多的事情,例如$matchWords 将只包含一个条目,它是 $skills 的当前值,因此您可能可以摆脱该代码(如果 preg_match 失败,则只需 continue 循环),并且因为仅 $matchWords有一个值,您可以使用调用preg_match 的函数执行preg_match 而不是array_filter。希望这会有所帮助。
  • if (count($missing)) $dev[$missing[0]] = $weight; 语句只是在尝试将值添加到$dev 数组之前检查$missing 中是否存在值。您的 array_combine 声明有效,但没有必要,因为 $missing 永远只有一个值(请参阅我之前的评论了解为什么会这样)。
【解决方案2】:

使用ksort

替换:

arsort($dev);

ksort($dev);

它会按键对数组进行排序

如果需要反转,使用array_reverse:

ksort($dev);
$dev = array_reverse($dev);

【讨论】:

  • 相同的输出。它比那更复杂;-)
  • 谢谢,但如前所述,我需要按降序排列的值。
  • 已编辑,看看。 3v4l.org/HdumD 但结果与您在问题中预期的结果相同
【解决方案3】:

自定义排序函数

uasort( $dev, function ( $a, $b ) {
    if ( intval( $a ) === intval( $b ) ) {
        return 0;
    }
    return intval( $a ) > intval( $b ) ? -1 : +1;
} );

【讨论】:

    猜你喜欢
    • 2014-02-16
    • 2013-09-20
    • 1970-01-01
    • 2016-03-10
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 2011-02-09
    • 2021-12-28
    相关资源
    最近更新 更多