Is there a function to make a copy of a PHP array to another?

前端 未结 19 2263
我寻月下人不归
我寻月下人不归 2020-12-07 06:44

Is there a function to make a copy of a PHP array to another?

I have been burned a few times trying to copy PHP arrays. I want to copy an array defined inside an obj

19条回答
  •  忘掉有多难
    2020-12-07 07:34

    Since this wasn't covered in any of the answers and is now available in PHP 5.3 (assumed Original Post was using 5.2).

    In order to maintain an array structure and change its values I prefer to use array_replace or array_replace_recursive depending on my use case.

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

    Here is an example using array_replace and array_replace_recursive demonstrating it being able to maintain the indexed order and capable of removing a reference.

    http://ideone.com/SzlBUZ

    The code below is written using the short array syntax available since PHP 5.4 which replaces array() with []. http://php.net/manual/en/language.types.array.php

    Works on either offset indexed and name indexed arrays

    $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);
    

    Output:

    ["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"
      }
    

提交回复
热议问题