14

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));
4
  • 1
    php.net/manual/en/function.sort.php sort returns true on success and false on failure... Commented Mar 10, 2015 at 7:13
  • This is the most visited question for the problem. However silly it is, 20 thousand people had this same question and searched for the answer. Which means this question is useful. Which means it is not off topic or should be closed or get whatever else punishment. Instead, it should be left alone or improved, to help future visitors. Which is the bloody mission of this bloody site of Stack Overflow Commented Feb 13 at 10:22
  • @You you may or may not notice that I do a lot of "heaping" while I perform my curation routines. Since finding those 85 sign posts, I've found many more. Because of the way I hunt for lost, misunderstood, and neglected PHP content, sometimes I don't find the best canonical until after I've hammered a dozen or two pages. I try to find the earliest clear minimal reproducible example as the sign post. I agree, this page has had lots of page views along with high scoring. I did not set out to make SO worse. You don't need to be so outraged, bold, and bloody. Commented Feb 13 at 11:34
  • @mickmackusa this question got a close vote very recently, only because of a silly typo in the code, which is TOTALLY unrelated to the question. It's that infamous pedantic attitude (which makes sense towards new questions but being a disaster towards established top links from Google search) that already ruined many posts. I don't know who cast this vote but my comment was for them. Just to remind that the mission of this site is not to punish people who dared to ask or search for answer Commented Feb 13 at 11:42

2 Answers 2

44

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

Sign up to request clarification or add additional context in comments.

1 Comment

still returning 1 the sort function
0

@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.

1 Comment

Its is returning 1 because you are printting the returned values by sort method. All these sorting method in Php return boolean values rather than sorted array. Now the question is how to get the result than well its simple see EXAMPLE below : $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 .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.