duanchanguo7603 2014-01-21 15:55
浏览 204
已采纳

如何从foreach循环深入复制PHP中的数组?

Here is the php code:

$options_schools = array();
$options_schools_deepcopy = array();

if (!empty($schools) && is_array($schools)) {
    foreach ($schools as $key => $val) {
    $temp_school = $key;
    $options_schools[$temp_school]=$key;    
    }
        $options_schools_deepcopy= $options_schools;
    }

echo form_dropdown('school', $options_schools_deepcopy, '');    

I want the array $options_schools_deepcopy to bear the deep copy values of array $options_schools with no reference to it. So, somewhere in the code when array $options_schools becomes null, $options_schools_deepcopy should still have the values originally copied from $options_schools irrespective of where it is accessed in the code. How to achieve it?

Edit1: Please note: As you see from my code, I am trying to make a copy of an array $a into $b while in a if-else condition. I want $b to have the same value of array $a which is assigned when if-else was satisfied. I want $b to retain the copied array anywhere in the code irrespective of it satisfies if-else condition or not and also no matter how array $a changes.

Edit2: if-else does become true but only at a certain point of the code and that is when $options_schools has all the values I need to get copied to $options_schools_deepcopy.

  • 写回答

2条回答 默认 最新

  • dtnpzghys01643322 2014-01-21 16:19
    关注

    Really there is no need to try to deep copy an array in php. Php uses a method called copy on write and reference counting when dealing with arrays. What does this mean? It means that unless you do $options_schools_deepcopy = &$options_schools you will in essence get a deep copy of the array, in that any modifications to values inside the $options_schools_deepcopy will be automatically copied into a new space in memory if there is a change made to either array. For example, consider the following code:

    $array1 = array("val1" => 1, "val2" => 2);
    $array2 = &$array1;
    
    $array2['val2']++;
    echo "Saved as Reference:
    ";
    echo $array2['val2'], "
    ";
    echo $array1['val2'], "
    ";
    
    unset($array1);
    unset($array2);
    
    $array1 = array("val1" => 1, "val2" => 2);
    $array2 = $array1;
    
    $array2['val2']++;
    echo "Saved as Value:
    ";
    echo $array2['val2'], "
    ";
    echo $array1['val2'], "
    ";
    
    unset($array1);
    
    var_dump($array2);
    

    In the Saved as Reference part you get exactly what you would expect if this were a language like java. You get one array with two references pointing at it and the $array2 reference can directly modify the data that $array1 points to. So any modification to the array through $array2 is reflected in $array1.

    However, in the Saved as Value part, you don't get this behavior. Instead what you get is two references pointing at the created array(before any modifications to the array are made). In this case, when you try to modify $array2['val2'], the copy on write aspect of php comes into play, which copies the original array into a newly allocated space of memory, points $array2 to this newly allocated spot then makes the update to $array2['val2'].

    So as you can see, there is really no need to try to make a deep copy of an array in php because php will already do that for you behind the scenes, so long as you don't specify that a variable should be a reference to said array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用