略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: ArrayIterator::getArrayCopy

2024-11-23

ArrayIterator::getArrayCopy

(PHP 5, PHP 7, PHP 8)

ArrayIterator::getArrayCopyGet array copy

说明

public ArrayIterator::getArrayCopy(): array

Get a copy of an array.

警告

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

A copy of the array, or array of public properties if ArrayIterator refers to an object.

参见

add a noteadd a note

User Contributed Notes 2 notes

up
2
lenye01 at gmail dot com
11 years ago
the difference of this method and the direct assign the object to a value is as follows:

<?php
$b
= array('name'=>'mengzhi','age'=>'12','city'=>'shanghai');
$a = new ArrayIterator($b);
$a->append(array('home'=>'china','work'=>'developer'));
$c = $a->getArrayCopy();
var_dump($a);
var_dump($c);
?>
result:
object(ArrayIterator)#1 (1) { ["storage":"ArrayIterator":private]=> array(4) { ["name"]=> string(7) "mengzhi" ["age"]=> string(2) "12" ["city"]=> string(8) "shanghai" [0]=> array(2) { ["home"]=> string(5) "china" ["work"]=> string(9) "developer" } } }

array(4) { ["name"]=> string(7) "mengzhi" ["age"]=> string(2) "12" ["city"]=> string(8) "shanghai" [0]=> array(2) { ["home"]=> string(5) "china" ["work"]=> string(9) "developer" } }
up
1
irvine L
4 years ago
Just in case some of you out there do NOT know this:

(a). 'getArrayCopy()', returns a copy of the ORIGINAL array - iterator object. Therefore, using (for example) 'LimitIterator' on an array-object, and then calling 'getArrayCopy' afterwards, might not return the current (adjusted) object.

(b). Instead, use the 'iterator_to_array' function, in order to access, or return, the current state of the array-object-iterator (whatever). Using the example above (in '(a)'); passing the 'LimitIterator' object into 'iterator_to_array', should return the CURRENT, and NOT ORIGINAL state of your array (iterator object).

官方地址:https://www.php.net/manual/en/arrayiterator.getarraycopy.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3