简体   繁体   English

带sort(array)的意外结果

[英]unexpected results with sort(array)

Odd..... 奇.....

// array sort test

$_ar = array(
  0 => "2015-02-23",
  1 => "2015-02-21",
  2 => "2015-02-28",
  3 => "2015-03-20",
  4 => "2015-03-14",
  5 => "2015-03-21",
  6 => "2015-02-21",
  7 => "2015-02-28",
  8 => "2015-03-07",
  9 => "2015-03-14",
);
$_ar = sort($_ar);

var_dump($_ar);
// returns bool(true)

$__ar = array(
  0 => "2015 02 23",
  1 => "2015 02 21",
  2 => "2015 02 28",
  3 => "2015 03 20",
  4 => "2015 03 14",
  5 => "2015 03 21",
  6 => "2015 02 21",
  7 => "2015 02 28",
  8 => "2015 03 07",
  9 => "2015 03 14",
);
$__ar = sort($__ar);

var_dump($__ar);
// returns bool(true)

$ar = array(
  0 => "20150223",
  1 => "20150221",
  2 => "20150228",
  3 => "20150320",
  4 => "20150314",
  5 => "20150321",
  6 => "20150221",
  7 => "20150228",
  8 => "20150307",
  9 => "20150314",
);

$ar = sort($ar);

var_dump($ar);
// returns bool(true)

I am expecting this to return the array sorted by the date value. 我期望这将返回按日期值排序的数组。 I thought maybe it was the - (hyphen) or spaces, but in all my examples my PHP var_dump simply returns bool(true) for each instance. 我以为可能是-(连字符)或空格,但是在我的所有示例中,我的PHP var_dump仅为每个实例返回bool(true)。 Can someone confirm they get the same, or point out what I must be missing.... I have tried asort() - still the same. 有人可以确认他们得到相同的结果,还是指出我必须缺少的内容...。我尝试过asort()-仍然相同。

You don't have to assign the return value of sort() . 您不必分配sort()的返回值。 For more information about sort() see the manual: http://php.net/manual/en/function.sort.php 有关sort()更多信息,请参见手册: http : //php.net/manual/en/function.sort.php

And a quote from there: 从那里引用:

Returns TRUE on success or FALSE on failure. 成功返回TRUE,失败返回FALSE。

So just to this: 所以就这样:

sort($_ar);

Side Note: 边注:

I wouldn't recommend you to define variables with underscores at the start of the name, since this already get's used by defined php variables eg super globals or magic constants 我不建议您在名称的开头定义带下划线的变量,因为已被定义的php变量(例如,超全局变量或魔术常数)所使用。

The sort and asort function returns bool value. sort and asort函数返回布尔值。 Just call this function and it will sort the array, don't store it , it returns true or false.Use the code below 只需调用此函数,它将对数组排序,不存储它,则返回true或false。使用下面的代码

// array sort test

$_ar = array(
  0 => "2015-02-23",
  1 => "2015-02-21",
  2 => "2015-02-28",
  3 => "2015-03-20",
  4 => "2015-03-14",
  5 => "2015-03-21",
  6 => "2015-02-21",
  7 => "2015-02-28",
  8 => "2015-03-07",
  9 => "2015-03-14",
);
 sort($_ar);

var_dump($_ar);
// returns bool(true)

$__ar = array(
  0 => "2015 02 23",
  1 => "2015 02 21",
  2 => "2015 02 28",
  3 => "2015 03 20",
  4 => "2015 03 14",
  5 => "2015 03 21",
  6 => "2015 02 21",
  7 => "2015 02 28",
  8 => "2015 03 07",
  9 => "2015 03 14",
);
$__ar = sort($__ar);

var_dump($__ar);
// returns bool(true)

$ar = array(
  0 => "20150223",
  1 => "20150221",
  2 => "20150228",
  3 => "20150320",
  4 => "20150314",
  5 => "20150321",
  6 => "20150221",
  7 => "20150228",
  8 => "20150307",
  9 => "20150314",
);

sort($ar);

var_dump($ar);
// returns bool(true)

Hope this helps you 希望这对您有帮助

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

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