Thursday, October 9, 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
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6712 POSTS0 COMMENTS
Nicole Veronica
11875 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6833 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS