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条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行