Below is a php variable numbers which contains an array i need to sort in Ascending order and result which i want is below that. Please help. $numbers = array(3.11,1, 2, 3.1,3.10,3,3.2,3.3);
result i want:
1 2 3 3.1 3.2 3.3 3.10 3.11
Below is a php variable numbers which contains an array i need to sort in Ascending order and result which i want is below that. Please help. $numbers = array(3.11,1, 2, 3.1,3.10,3,3.2,3.3);
result i want:
1 2 3 3.1 3.2 3.3 3.10 3.11
If you setup your array with strings, you can use natsort function to get result you want
$numbers = array('1', '2', '3', '3.1', '3.2', '3.3', '3.10', '3.11');
natsort($numbers);
print_r($numbers);
Now, when you init array with numbers, there is no difference between 3.10 and 3.1
You can use sort()
function
$numbers = array(1, 2, 3, 3.1, 3.2, 3.3, 3.10, 3.11);
sort($numbers);
var_dump($numbers);
sort() function
.For e.g.
$numbers = array(1, 2, 3, 3.1, 3.2, 3.3, 3.10, 3.11);
sort($numbers);
print_r($numbers);
Output: 1 2 3 3.1 3.2 3.3 3.10 3.11