Array is not getting sorted with sort() function PHP

Viewed 67

I'm trying to sort an array numerically. Here's my code

<?php
$data = '9#Saul,7#Jesse,1#Skyler,6#Walter';
$exp = explode(",",$data);
$expsort = sort($exp);
print_r($expsort);
?>

But it is not working. The output is showing only "1".

1 Answers

You are assigning the value of the sort function -which sorts the argument array itself, and it always returns true and thus you got 1 as a result.

So if you print your original exploded array, it will be sorted. Please note, sort overrides your original array

$data = '9#Saul,7#Jesse,1#Skyler,6#Walter';
$exp = explode(",",$data);
sort($exp);
print_r($exp);
Related