Sunday, December 14, 2025
HomeLanguagesPHP | ArrayIterator getArrayCopy() Function

PHP | ArrayIterator getArrayCopy() Function

The ArrayIterator::getArrayCopy() function is an inbuilt function in PHP which is used to create a copy of an array.

Syntax:

array ArrayIterator::getArrayCopy( void )

Parameters: This function does not accept any parameters.

Return Value: This function returns the copy of array.

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

Program 1:




<?php
   
// Declare an ArrayIterator
$arrItr = new ArrayIterator(
    array('G', 'e', 'e', 'k', 's', 'f', 'o', 'r')
);
  
// Create a copy of array
$copyArr = $arrItr->getArrayCopy();
  
// Display the array iterator element
var_dump($copyArr);
   
?>


Output:

array(8) {
  [0]=>
  string(1) "G"
  [1]=>
  string(1) "e"
  [2]=>
  string(1) "e"
  [3]=>
  string(1) "k"
  [4]=>
  string(1) "s"
  [5]=>
  string(1) "f"
  [6]=>
  string(1) "o"
  [7]=>
  string(1) "r"
}

Program 2:




<?php
   
// Declare an ArrayIterator
$arrItr = new ArrayIterator(
    array("Geeks", "for", "Geeks")
);
   
// Append the element into array
$arrItr->append("Computer");
$arrItr->append("Science");
$arrItr->append("Portal");
  
// Create copy of array iterator
$copyArr = $arrItr->getArrayCopy();
  
// Display the array element 
var_dump($copyArr);
  
?>


Output:

array(6) {
  [0]=>
  string(5) "Geeks"
  [1]=>
  string(3) "for"
  [2]=>
  string(5) "Geeks"
  [3]=>
  string(8) "Computer"
  [4]=>
  string(7) "Science"
  [5]=>
  string(6) "Portal"
}

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

RELATED ARTICLES

Most Popular

Dominic
32447 POSTS0 COMMENTS
Milvus
105 POSTS0 COMMENTS
Nango Kala
6816 POSTS0 COMMENTS
Nicole Veronica
11953 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12031 POSTS0 COMMENTS
Shaida Kate Naidoo
6951 POSTS0 COMMENTS
Ted Musemwa
7202 POSTS0 COMMENTS
Thapelo Manthata
6898 POSTS0 COMMENTS
Umr Jansen
6882 POSTS0 COMMENTS