0

I've been trying to use the sort() function to rearrange the array from smallest to largest.

This is my print_r of the array which came from serialized data that was imploded:

Array
    (
    [0] => 127173
    [1] => 127172
    [2] => 127174
    [3] => 127175
    [4] => 127178
    [5] => 127176
    [6] => 127177
    [7] => 127179
    [8] => 127180
    [9] => 127183
    [10] => 127181
)

With sort() and asort() I just get a 1 returning.

2
  • please give me your full php code Commented Jul 17, 2015 at 3:31
  • 2
    You problem is not clear. what is your issue ? Commented Jul 17, 2015 at 3:54

2 Answers 2

2

Try this code... in fact the sort function is working fine.

$array = Array
    (
    '0' => 127173,
    '1' => 127172,
    '2' => 127174,
    '3' => 127175,
    '4' => 127178,
    '5' => 127176,
    '6' => 127177,
    '7' => 127179,
    '8' => 127180,
    '9' => 127183,
    '10' => 127181
    );

sort($array); // <= Sort the array desc

foreach( $array as $key => $value ){
    echo $key."\t=>\t".$value."\n";
}

Consider that sort function actually alters your array and returns bool. See doc.

Check this example online

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

1 Comment

I see what happened I had done the following: $results = sort($results); That's what kept making it come out at 1. Doing it just sort($results); Worked like a charm. Thanks for the help all!
0

Use asort(), like this:

$A = Array (127173,127172,127174,127175,127178,127176,127177,127179,127180,127183,127181);
asort($A);
print_r($A);

Result:

Array ( [1] => 127172 [0] => 127173 [2] => 127174 [3] => 127175 [5] => 127176 [6] => 127177 [4] => 127178 [7] => 127179 [8] => 127180 [10] => 127181 [9] => 127183 )

Compare sorting-functions here: http://php.net/manual/en/array.sorting.php

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.