Friday, December 12, 2025
HomeLanguagesPHP | ArrayIterator natsort() Function

PHP | ArrayIterator natsort() Function

The ArrayIterator::natsort() function is an inbuilt function in PHP which is used to sort an array naturally.

Syntax:

void ArrayIterator::natsort( void )

Parameters: This function does not accept any parameters.

Return Value: This function does not return any value.

Below programs illustrate the ArrayIterator::natsort() function in PHP:

Program 1:




<?php
    
// Declare an ArrayIterator
$arrItr = new ArrayIterator(
    array(
        5 => 'G',
        4 => 'e',
        3 => 'E',
        2 => 'k',
        1 => 'S'
    )
);
    
// Sort the array key
$arrItr->natsort();
   
// Display the element
while($arrItr->valid()) {
    echo $arrItr->current() . " ";
    $arrItr->next();
}
    
?>


Output:

E G S e k

Program 2:




<?php
   
// Declare an ArrayIterator
$arrItr = new ArrayIterator(
    array("neveropen", "GEEKS", "Geeks", "gEEKS")
);
  
// Sort the array with case sensitive
$arrItr->natsort();
  
var_dump($arrItr);
  
?>


Output:

object(ArrayIterator)#1 (1) {
  ["storage":"ArrayIterator":private]=>
  array(4) {
    [1]=>
    string(5) "GEEKS"
    [2]=>
    string(5) "Geeks"
    [3]=>
    string(5) "gEEKS"
    [0]=>
    string(5) "neveropen"
  }
}

Reference: https://www.php.net/manual/en/arrayiterator.natsort.php

RELATED ARTICLES

Most Popular

Dominic
32445 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6814 POSTS0 COMMENTS
Nicole Veronica
11952 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12029 POSTS0 COMMENTS
Shaida Kate Naidoo
6949 POSTS0 COMMENTS
Ted Musemwa
7199 POSTS0 COMMENTS
Thapelo Manthata
6895 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS