php sort($ array)返回1而不是排序数组

Rav*_*ane 8 php session

我正在尝试对数组进行排序.当我将排序结果打印到屏幕时,它会打印出来1.为什么打印1而不是排序数组的内容?

这是我的代码:

session_start();
if (isset($_POST))
{
     $_SESSION['total_elements'];
     $value1 = $_POST["username"];


     if (isset($_SESSION['total_elements']))
     {
         if (!empty($value1))
         {
             array_push($_SESSION['total_elements'], $value1);
         }
     }
}
$a = array();
$a = $_SESSION['total_elements'];
print_r($asceding_order) = sort($a); // printing 1
Run Code Online (Sandbox Code Playgroud)

Han*_*nky 23

sort只对数组进行排序,不返回它:)它返回booleanTRUE给你,你的echo显示为1

echo $asceding_order= sort($a);   // wrong
Run Code Online (Sandbox Code Playgroud)

正确的方法是

sort($a);
print_r($a);
Run Code Online (Sandbox Code Playgroud)

这是函数原型供参考

bool sort(array&$ array [,int $ sort_flags = SORT_REGULAR])

  • 排序函数仍然返回 1 (2认同)