Sort numbers in a session?

3

I have a table where users select multiple numbers and are written to $_SESSION , so that's fine, but I need these numbers to appear in ascending order:

Example of $_SESSION gravada = 22-20-38-54-52-42-34-18-70-75

I need to appear like this = 18-20-22-34-38-42-52-54-70-75

    
asked by anonymous 21.06.2017 / 18:13

1 answer

3

Do the following, transform this string into an array, then sort the array the way you want and finally convert the array to a string separated by the character you want.

$_SESSION['numbers'] = '22-20-38-54-52-42-34-18-70-75';
$array_numbers = explode( '-', $_SESSION['numbers'] ); // Quebra a string em um array

sort( $array_numbers ); // Ordena o array em ordem crescente

$numbers = implode( '-', $array_numbers ); // Quebra do array em string, separando por "-"

print_r( $numbers ); // Vai imprimir o seguinte resultado: 18-20-22-34-38-42-52-54-70-75
    
21.06.2017 / 18:25