Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...