I am trying to sort an array. When I print the sort results to screen it prints 1. Why does it print 1 instead of the contents of the sorted array?
Here is my code:
$a = array();
$a = $_SESSION['total_elements'];
print_r(sort($a));
I am trying to sort an array. When I print the sort results to screen it prints 1. Why does it print 1 instead of the contents of the sorted array?
Here is my code:
$a = array();
$a = $_SESSION['total_elements'];
print_r(sort($a));
sort just sorts the array, doesn't return it :) It is returning boolean TRUE to you which your echo is showing as 1
echo $asceding_order= sort($a); // wrong
Right way would be
sort($a);
print_r($a);
Here is the function prototype for reference
bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
@Fernando - instead of using:
print_r($asceding_order) = sort($a);
or assigning value, just do:
$a = $_SESSION['total_elements'];
sort($a);
This will sort the array and return it.
$arr = array(1,5,7,4,9,2,88); // **Original Array** echo "<pre>" print_r($arr); **sort($arr);** // **Sorted Array** echo "<pre>" print_r($arr); NOTE : you do not need to store the sorted value in any other variable just use same array name .