Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a php array returning new array

I am looking for a reliable standard method to sort an array, returning the sorted ( associative ) array, as return value.

All the PHP.net functions I have read about return BOOLEAN value, or 0-1. The method I need would be something like:

$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 )

No need to handle strings, just INT-s.

like image 887
István Pálinkás Avatar asked Sep 10 '13 13:09

István Pálinkás


People also ask

What does sort return in PHP?

Return Value: It returns a boolean value, TRUE on success and False in failure. It sorts the original array in ascending order which is passed as a parameter.

How can we sort an array without using sort method in PHP?

php function sortArray() { $inputArray = array(8, 2, 7, 4, 5); $outArray = array(); for($x=1; $x<=100; $x++) { if (in_array($x, $inputArray)) { array_push($outArray, $x); } } return $outArray; } $sortArray = sortArray(); foreach ($sortArray as $value) { echo $value . "<br />"; } ?>


1 Answers

Here's a one-liner:

call_user_func(function(array $a){asort($a);return $a;}, $some_mixed_array);

like image 121
Ben Avatar answered Sep 30 '22 14:09

Ben