Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort( $new, SORT_NATURAL | SORT_FLAG_CASE ) in PHP 5.3

Tags:

php

sorting

sort( $new, SORT_NATURAL | SORT_FLAG_CASE );

SORT_NATURAL is new in php 5.4 but i have 5.3.10 running on my localhost (ubuntu 12.04) not really intention to upgrade because of that.

What would be the equivalent in php 5.3, i have read that is just like natsort.

is natsort( $new, SORT_FLAG_CASE ); the same ?

like image 206
James Mitch Avatar asked Mar 15 '13 19:03

James Mitch


2 Answers

The PHP Manual points out that natsort ($array) is the equivalent of sort($array,SORT_NATURAL); it also points that SORT_FLAG_CASE wasn't added until 5.4.0 either.

You can use natcasesort($array) which is the equivalent of sort($array,SORT_NATURAL | SORT_FLAG_CASE).

like image 135
Snivs Avatar answered Oct 17 '22 19:10

Snivs


While they are essentially the same, the important difference to note between natsort($array) and sort($array, SORT_NATURAL) is key associations.

natsort() maintains key associations, meaning that keys and values are kept linked.

However, sort() does NOT main associations, so values are reassigned keys based on their new order.

like image 34
Benjamin Fleet Avatar answered Oct 17 '22 19:10

Benjamin Fleet