Home » Php » sorting – sort( $new, SORT_NATURAL | SORT_FLAG_CASE ) in PHP 5.3

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

Posted by: admin July 12, 2020 Leave a comment

Questions:
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 ?

How to&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).

Answer:

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.