是否有一个函数可以将一个PHP数组复制到另一个数组?

我已经被烧毁了几次试图复制PHP数组。我想将对象内定义的数组复制到对象外部的全局数组。


当前回答

我发现最安全、最便宜的方法是:

<?php 
$b = array_values($a);

这也有重新索引数组的好处。

这将不会像预期的那样工作在关联数组(哈希)上,但也不是以前的大多数答案。

其他回答

因为这在任何答案中都没有涉及,现在在PHP 5.3中可用(假设原始帖子使用5.2)。

为了维护数组结构并更改其值,我更喜欢使用array_replace或array_replace_recursive,这取决于我的用例。

http://php.net/manual/en/function.array-replace.php

下面是一个使用array_replace和array_replace_recursive的示例,演示它能够维护索引顺序并能够删除引用。

http://ideone.com/SzlBUZ

下面的代码使用PHP 5.4以来可用的短数组语法编写,该语法将array()替换为[]。 http://php.net/manual/en/language.types.array.php

适用于偏移量索引数组和名称索引数组

$o1 = new stdClass;
$a = 'd';
//This is the base array or the initial structure
$o1->ar1 = ['a', 'b', ['ca', 'cb']];
$o1->ar1[3] = & $a; //set 3rd offset to reference $a

//direct copy (not passed by reference)
$o1->ar2 = $o1->ar1; //alternatively array_replace($o1->ar1, []);
$o1->ar1[0] = 'z'; //set offset 0 of ar1 = z do not change ar2
$o1->ar1[3] = 'e'; //$a = e (changes value of 3rd offset to e in ar1 and ar2)

//copy and remove reference to 3rd offset of ar1 and change 2nd offset to a new array
$o1->ar3 = array_replace($o1->ar1, [2 => ['aa'], 3 => 'd']);

//maintain original array of the 2nd offset in ar1 and change the value at offset 0
//also remove reference of the 2nd offset
//note: offset 3 and 2 are transposed
$o1->ar4 = array_replace_recursive($o1->ar1, [3 => 'f', 2 => ['bb']]);

var_dump($o1);

输出:

["ar1"]=>
  array(4) {
    [0]=>
    string(1) "z"
    [1]=>
    string(1) "b"
    [2]=>
    array(2) {
      [0]=>
      string(2) "ca"
      [1]=>
      string(2) "cb"
    }
    [3]=>
    &string(1) "e"
  }
  ["ar2"]=>
  array(4) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
    [2]=>
    array(2) {
      [0]=>
      string(2) "ca"
      [1]=>
      string(2) "cb"
    }
    [3]=>
    &string(1) "e"
  }
  ["ar3"]=>
  array(4) {
    [0]=>
    string(1) "z"
    [1]=>
    string(1) "b"
    [2]=>
    array(1) {
      [0]=>
      string(2) "aa"
    }
    [3]=>
    string(1) "d"
  }
  ["ar4"]=>
  array(4) {
    [0]=>
    string(1) "z"
    [1]=>
    string(1) "b"
    [2]=>
    array(2) {
      [0]=>
      string(2) "bb"
      [1]=>
      string(2) "cb"
    }
    [3]=>
    string(1) "f"
  }
private function cloneObject($mixed)
{
    switch (true) {
        case is_object($mixed):
            return clone $mixed;
        case is_array($mixed):
            return array_map(array($this, __FUNCTION__), $mixed);
        default:
            return $mixed;
    }
}

如果您有一个包含对象的数组,您需要在不触及其内部指针的情况下复制该数组,并且需要克隆所有对象(这样当您对复制的数组进行更改时不会修改原始对象),请使用此方法。

不接触数组内部指针的技巧是确保您使用的是数组的副本,而不是原始数组(或原始数组的引用),因此使用函数形参将完成工作(因此,这是一个接受数组的函数)。

注意,如果你希望对象的属性也被克隆,你仍然需要在对象上实现__clone()。

此函数适用于任何类型的数组(包括混合类型)。

function array_clone($array) {
    return array_map(function($element) {
        return ((is_array($element))
            ? array_clone($element)
            : ((is_object($element))
                ? clone $element
                : $element
            )
        );
    }, $array);
}

定义:

$copy = create_function('$a', 'return $a;');

复制$_ARRAY到$_ARRAY2:

$_ARRAY2 = array_map($copy, $_ARRAY);

我很久以前就知道这一点,但这对我来说很有效。

$copied_array = array_slice($original_array,0,count($original_array));