Search code examples
phparrayssorting

sort() is not returning the sorted array


I want to sort an array that looks like this (to numerical order instead of 1, 10, 11):

Array ( [0] => 1.jpg [1] => 10.jpg [2] => 11.jpg [3] => 111.jpg [4] => 12.jpg [5] => 12a.jpg [6] => 13.jpg [7] => 14.jpg [8] => 15.jpg [9] => 16.jpg [10] => 2.jpg [11] => 3.jpg [12] => 4.jpg [13] => 5.jpg [14] => 6.jpg [15] => 7.jpg [16] => 8.jpg [17] => 9.jpg )

when i use sort() it just becomes "1" instead of a sorted array.

code:

$this->pageLinks = sort($this->pageLinks);  // the array is a property in a class     
print_r($this->pageLinks); // want to display the sorted array here but it just returns 1

Solution

  • sort() sorts the array in-place. Don't re-assign it.

    Correct:

    sort($this->pageLinks);
    

    Incorrect:

    $this->pageLinks = sort($this->pageLinks);