Ste*_*kas 8 php arrays sorting associative-array
我正在寻找一种可靠的标准方法来对数组进行排序,将有序(关联)数组作为返回值返回.
我读过的关于返回BOOLEAN值的所有PHP.net函数,或0-1.我需要的方法是这样的:
$some_mixed_array = array( 998, 6, 430 );
function custom_sort( $array )
{
// Sort it
// return sorted array
}
custom_sort( $some_mixed_array );
// returning: array( 6, 430, 998 )
Run Code Online (Sandbox Code Playgroud)
不需要处理字符串,只需INT-s.
你能做到吗?
$some_mixed_array = array( 998, 6, 430 );
function custom_sort( $array )
{
// Sort it
asort($array);
// return sorted array
return $array;
}
custom_sort( $some_mixed_array );
// returning: array( 6, 430, 998 )
Run Code Online (Sandbox Code Playgroud)
这也可以解决您的问题:
$some_mixed_array = array( 998, 6, 430 );
echo '<pre>'.print_r($some_mixed_array, true).'</pre>';
asort($some_mixed_array); // <- BAM!
// returning: array( 6, 430, 998 )
echo '<pre>'.print_r($some_mixed_array, true).'</pre>';
Run Code Online (Sandbox Code Playgroud)