PHP中ArrayObject exchangeArray()函数的使用详解

PHP中ArrayObject类的exchangeArray()函数用于从ArrayObject交换数组。也就是说,它用一个新描述的数组替换来自ArrayObject的现有数组。

语法:

ArrayObject exchangeArray( $inputArray )

参数:这个函数接受一个参数$inputArray,它是一个新数组,旧数组将与之在ArrayObject中交换。

返回值:此函数返回旧数组。

示例1:

  1. <?php
  2. $arr = array("a" => "geeks", "b" => "are", "c" => "awesome");
  3. // 创建数组对象
  4. $arrObject = new ArrayObject($arr);
  5. // 新数组
  6. $newArr = array("1" => "New", "2" => "Array");
  7. // 在ArrayObject中交换数组
  8. $arrObject->exchangeArray($newArr);
  9. print_r($arrObject);

输出:

  1. ArrayObject Object
  2. (
  3. [storage:ArrayObject:private] => Array
  4. (
  5. [1] => New
  6. [2] => Array
  7. )
  8. )

示例2:

  1. $arr = array("a" => "Welcome", "b" => "2", "c" => "GFG");
  2. $arrObject = new ArrayObject($arr);
  3. $newArr = array("1" => "Hello", "2" => "World");
  4. $arrObject->exchangeArray($newArr);
  5. print_r($arrObject);

输出:

  1. ArrayObject Object
  2. (
  3. [storage:ArrayObject:private] => Array
  4. (
  5. [1] => Hello
  6. [2] => World
  7. )
  8. )