Php 按降序排序数组值

Php 按降序排序数组值,php,arrays,sorting,Php,Arrays,Sorting,如何按降序对值进行排序?我试过阿索特;但这在我的情况下不起作用: $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", "ang

如何按降序对值进行排序?我试过阿索特;但这在我的情况下不起作用:

$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>";

}
输出:

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

但我想得到这个结果:

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

编辑:

我还没有找到建议的答案。

使用

替换:

arsort($dev);

它将按键对数组进行排序

如果需要反转,请使用:


自定义排序函数

uasort( $dev, function ( $a, $b ) {
    if ( intval( $a ) === intval( $b ) ) {
        return 0;
    }
    return intval( $a ) > intval( $b ) ? -1 : +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>";
}

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

相同的输出。这比那更复杂-谢谢,但如前所述,我需要按顺序排列的值。已编辑,请查看。但是结果与你在问题中所期望的结果是一样的,可能是重复的,谢谢,尼克,这对我有用。尽管我对此感到困惑:如果count$missing$dev[$missing[0]]=$weight;我也不明白为什么来自“$dev=array\u combine$missing,$weight;”的数组没用。我会研究这个。任何简化循环的提示都将不胜感激。嗨,Seb,我认为如果你在循环中加入调试打印,你会发现很多代码都做不到什么,例如,$matchWords将只包含一个条目,它是$skills的当前值,因此如果preg_匹配失败,你可能可以删除该代码并继续循环,而且,由于$matchWords只有一个值,您可以只进行preg_匹配,而不是使用调用preg_匹配的函数进行数组过滤器。希望这有帮助。如果计数$missing$dev[$missing[0]]=$weight;语句只是在尝试将值添加到$dev数组之前检查$missing中是否有值。您的array_combine语句可以工作,但没有必要,因为$missing将只有一个值。请参阅我前面的注释以了解原因。
$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