Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

php - Sorting array value in descending order

How can I sort the values in descending order? I've tried arsort(); but it doesn't work in my case:

$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("~$skills~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('/'.preg_quote($w, '/').'/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>";

}

Output:

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

But I'd like to get this result:

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

EDIT:

I haven't found an answer here as suggested.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The reason your array is not being sorted correctly is because you are attempting to sort a single element array. If you put debugging prints in your code you will see that $dev only ever has one element when you call arsort. You need to assemble $dev in the loop, then sort it afterwards. Try something like this:

$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("~$skills~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('/'.preg_quote($w, '/').'/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>";
}

Output:

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

Your loop code could be simplified greatly but that's another question...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...