按降序对数组值进行排序 [英] Sorting array value in descending order
问题描述
如何按降序对值进行排序?我已经尝试过arsort();但这对我来说不起作用:
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("~\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
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:
键:Android SDK值:3 关键:android studio价值:3 密钥:angular js值:3 关键点:角度值:2 密钥:应用程序值:1
Key: Android SDK Value: 3 Key: android studio Value: 3 Key: angular js Value: 3 Key: angular Value: 2 Key: application Value: 1
我没有在建议的此处找到答案. >
I haven't found an answer here as suggested.
推荐答案
未正确排序数组的原因是因为您正在尝试对单个元素数组进行排序.如果在代码中放入调试打印,则在调用arsort
时将看到$dev
仅包含一个元素.您需要在循环中组装$dev
,然后对其进行排序.尝试这样的事情:
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("~\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
您的循环代码可以大大简化,但这是另一个问题...
Your loop code could be simplified greatly but that's another question...
这篇关于按降序对数组值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!