简体   繁体   English

排序数组(“1:40”,“2:30”,“6:33”)从最低到最高?

[英]sort an array("1:40","2:30","6:33") lowest to highest?

I am new in PHP I have an array a different format but, I don't know how to sort from lowest to highest i know the inbuilt function like sort and others too but these are not suitable for me please help if anybody knows, I have this array format我是 PHP 的新手,我有一个格式不同的数组,但是,我不知道如何从最低到最高排序,我知道内置的 function 也喜欢排序和其他,但这些不适合我,如果有人知道,请帮助,我有这种数组格式

$array = array("2:90","8:49","3:77","4:98");

and this array needs to sort as这个数组需要排序为

8:49,3:77,2:90,4:98

sort by right values after symbol (:)按符号 (:) 后的正确值排序

You could define a custom comparator and use usort with it:您可以定义一个自定义比较器并使用usort

<?php

function cmp($a, $b) {
    // Split the value by the ":" delimiter and store each side into variable
    list($a_first, $a_second) = explode(':', $a);
    list($b_first, $b_second) = explode(':', $b);
    // If the second substrings are not equal compare them
    if ($a_second != $b_second) {
      return strcmp($a_second, $b_second);
    }
    // If they are equal compare the values in the first substrings
    return strcmp($a_first, $b_first);
}

$array = array("2:90","1:90","8:49","3:77","4:98");
print "Before sorting:\n";
print_r($array);
usort($array, "cmp");
print "After sorting:\n";
print_r($array);

?>

Output: Output:

Before sorting: 
Array
(
    [0] => 2:90
    [1] => 1:90
    [2] => 8:49
    [3] => 3:77
    [4] => 4:98
)
After sorting: 
Array
(
    [0] => 8:49
    [1] => 3:77
    [2] => 1:90
    [3] => 2:90
    [4] => 4:98
)

Explanations and links to docs of other functions used:使用的其他功能的说明和文档链接:

  • explode - Split a string by a string. explode - 用一个字符串拆分一个字符串。
  • strcmp - Binary safe string comparison. strcmp - 二进制安全字符串比较。

A solution to the additional requirement to handle decimals provided in comments of this answer:此答案的评论中提供了处理小数的附加要求的解决方案:

<?php

function cmp($a, $b) {
    // Split the value by the ":" delimiter and store each side into variable
    list($a_first, $a_second) = array_map('floatval', explode(':', $a));
    list($b_first, $b_second) = array_map('floatval', explode(':', $b));
    // If the second substrings are not equal compare them
    if (abs($a_second - $b_second) >= PHP_FLOAT_EPSILON) {
      return $a_second - $b_second;
    }
    // If they are equal compare the values in the first substrings
    return $a_first - $b_first;
}

$array = array("2:90.60","2:95.67","1:95.67","8:49","3:77.66","4:98.30");
print "Before sorting:\n";
print_r($array);
usort($array, "cmp");
print "After sorting:\n";
print_r($array);

?> 

Output: Output:

Before sorting:
Array
(
    [0] => 2:90.60
    [1] => 2:95.67
    [2] => 1:95.67
    [3] => 8:49
    [4] => 3:77.66
    [5] => 4:98.30
)
After sorting:
Array
(
    [0] => 8:49
    [1] => 3:77.66
    [2] => 2:90.60
    [3] => 1:95.67
    [4] => 2:95.67
    [5] => 4:98.30
)

Explanations and links to docs of other functions used:使用的其他功能的说明和文档链接:

  • array_map - Applies the callback to the elements of the given arraysarray_map - 将回调应用于给定 arrays 的元素
  • floatval - Get float value of a variable floatval - 获取变量的浮点值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM