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.
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.
I just ran a little test:
$a = array(1 => array(1 => 'X'));
$b = $a;
$a[1][1] = 'Z';
echo $b[1][1]; // X
I knew that copying arrays was as simple as saying $b = $a
, but i wasn't sure about nested arrays. It turns out it is a the same.
The only items in an array that will copy like this are scalar values however.