English 中文(简体)
具有混合型数值的阵列
原标题:Numeric sort an array with mixed types values

我有这样的组合:

$fruits = array(
    "lemon",
    "Lemon",
    20,
    "banana",
    "apple",
    "121",
    40,
    50
);

之后,将<代码>ort()功能适用于:

sort($fruits, SORT_NUMERIC);
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "
";
}

现在,我不理解以下产出:

fruits[0] = apple
fruits[1] = lemon
fruits[2] = banana
fruits[3] = Lemon
fruits[4] = 20
fruits[5] = 40
fruits[6] = 50
fruits[7] = 121

请解释为什么会这样分类?

问题回答

OP, since sort(); 为什么不把任何事情转换到那么低的情况?

<?php
$fruits = array("lemon","Lemon", 20, "banana", "apple","121",40,50);
$fruits = array_map("strtolower", $fruits); //using strtolower

sort($fruits );
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "
";
}
?>

Outputs

fruits[0] = 20
fruits[1] = 40
fruits[2] = 50
fruits[3] = 121
fruits[4] = apple
fruits[5] = banana
fruits[6] = lemon
fruits[7] = lemon

基本上,它应当按数字顺序从A到Z进行分类,但使用混合型功能则不知道如何分类和随机结果。

手册中有大量警告说:

Be careful when sorting arrays with mixed types values because sort() can produce unpredictable results.

您可以增加职能的一个参数:

  • 缩略语

  • 缩略语

  • 缩略语

  • SORT_LOCALE_STRING - compare items as strings, based on the current locale. Added in PHP 4.4.0 and 5.0.2, it uses the system locale, which can be changed using setlocale().

根据手册here, php.net

Edit 1: Probably you can obtain the best sort result using the flag SORT_REGULAR because it does not change the variable type and numbers remain numbers and strings remain strings but it will also give you a strange result

fruits[0] = 121
fruits[1] = Lemon
fruits[2] = apple
fruits[3] = banana
fruits[4] = lemon
fruits[5] = 20
fruits[6] = 40
fruits[7] = 50

I think because it compare the ascii code from the letters of strings and L is before a b l... 121 is in first place because you wrote it like a string "121"

<><>><>>>>

进行这项工作的最佳方式是区分这些类型:(这样,网站将把“121”作为编号而不是说明,但你可以简单地通过if条款”决定。

<?php
$fruits = array("lemon","Lemon", 20, "banana", "apple","121",40,50);
$arr1=array();
$arr2=array();
      foreach($fruits as $key=>$val){
          if (is_numeric($val))
               array_push($arr1,$val); 
          else
              array_push($arr2,$val);
      }          
sort($arr1,SORT_NUMERIC);            
sort($arr2,SORT_LOCALE_STRING);
$fruits = array_merge($arr1,$arr2);
echo "<pre>";
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "
";
}
?>

我相信sort($array,SORT_NUMERIC),不能转换成一个编号的数值作为<0>处理,并按这一方式分类。 由于你的例子中的所有内容都变成了0,因此,在lemon之前,这完全不是问题。 例如:

<?php
$fruits = array("alpha", "bravo", -1, -0.001, 0, 0.001, 1, "x-ray", "zulu");
shuffle($fruits); // randomly re-arrange the items in array
sort($fruits, SORT_NUMERIC);
print_r($fruits);
// output -- pass 1
Array
(
    [0] => -1
    [1] => -0.001
    [2] => 0
    [3] => alpha
    [4] => x-ray
    [5] => zulu
    [6] => bravo
    [7] => 0.001
    [8] => 1
)
// output -- pass 2
Array
(
    [0] => -1
    [1] => -0.001
    [2] => bravo
    [3] => zulu
    [4] => 0
    [5] => x-ray
    [6] => alpha
    [7] => 0.001
    [8] => 1
)
// output -- pass 3
Array
(
    [0] => -1
    [1] => -0.001
    [2] => x-ray
    [3] => zulu
    [4] => 0
    [5] => bravo
    [6] => alpha
    [7] => 0.001
    [8] => 1
)
?>




相关问题
How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

Can I Nest OrderBy in .NET?

This doesn t seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m) m.Sent).First.Sent) For Each d As Discussion In x ....

sorting elements javascript

I m looking for a way to sort my elements, but it isn t as easy as it sounds. Please let me explain My elements are grouped per 6 elements (thumbnails), each x represents a thumbnail However all ...

热门标签