PHP中ArrayObject exchangeArray()函数的使用详解
PHP中ArrayObject类的exchangeArray()函数用于从ArrayObject交换数组。也就是说,它用一个新描述的数组替换来自ArrayObject的现有数组。
语法:
ArrayObject exchangeArray( $inputArray )
参数:这个函数接受一个参数$inputArray,它是一个新数组,旧数组将与之在ArrayObject中交换。
返回值:此函数返回旧数组。
示例1:
- <?php
- $arr = array("a" => "geeks", "b" => "are", "c" => "awesome");
- // 创建数组对象
- $arrObject = new ArrayObject($arr);
- // 新数组
- $newArr = array("1" => "New", "2" => "Array");
- // 在ArrayObject中交换数组
- $arrObject->exchangeArray($newArr);
- print_r($arrObject);
输出:
- ArrayObject Object
- (
- [storage:ArrayObject:private] => Array
- (
- [1] => New
- [2] => Array
- )
- )
示例2:
- $arr = array("a" => "Welcome", "b" => "2", "c" => "GFG");
- $arrObject = new ArrayObject($arr);
- $newArr = array("1" => "Hello", "2" => "World");
- $arrObject->exchangeArray($newArr);
- print_r($arrObject);
输出:
- ArrayObject Object
- (
- [storage:ArrayObject:private] => Array
- (
- [1] => Hello
- [2] => World
- )
- )