1

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.

sunskin
  • 1,620
  • 3
  • 25
  • 49
  • 1
    This may point you in the right direction. http://stackoverflow.com/questions/894814/deep-copy-of-php-array-of-references – Anil Jan 21 '14 at 16:00
  • Use `$options_schools_deepcopy[]= $options_schools;` instead of `$options_schools_deepcopy= $options_schools;`and put in into your loop. – Tounu Jan 21 '14 at 16:02
  • @Tounu I tried it. It doesn't work. sorry – sunskin Jan 21 '14 at 16:11
  • I actualy don't understand your problem. You want `$options_schools_deepcopy` to be equal to `$options_scools` ? Then your code works. I don't see where's your problem. – Tounu Jan 21 '14 at 16:17
  • Did you test? This code should already give you 2 independent arrays as you want. – Digital Chris Jan 21 '14 at 16:24
  • I tested Tounu suggestion. It doesn't seem to work in my case. – sunskin Jan 21 '14 at 16:27
  • I updated my question with "Edit1" to make my intention clear. – sunskin Jan 21 '14 at 16:30
  • If the if-else condition is false both arrays will be empty. This makes no sense. – Digital Chris Jan 21 '14 at 16:33
  • @Digital Chris: if-else does become true for sure 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 – sunskin Jan 21 '14 at 16:36
  • So what is your problem? What is the error you are getting? Be specific. Display what both arrays are, and what you think they should be. – Digital Chris Jan 21 '14 at 16:38

2 Answers2

4

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:\n";
echo $array2['val2'], "\n";
echo $array1['val2'], "\n";

unset($array1);
unset($array2);

$array1 = array("val1" => 1, "val2" => 2);
$array2 = $array1;

$array2['val2']++;
echo "Saved as Value:\n";
echo $array2['val2'], "\n";
echo $array1['val2'], "\n";

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.

elitechief21
  • 2,964
  • 1
  • 17
  • 15
  • +1 Good explanation! Thank you! However, my question has little more to it. Please see my updated question. I have added an "Edit1" to make my intention clear – sunskin Jan 21 '14 at 16:31
  • 2
    Doing this inside of your if statement $options_schools_deepcopy = $options_schools; should give you exactly what you want. So long as the if was satisfied and this line was executed $options_schools_deepcopy = $options_schools; then the $options_schools_deepcopy will contain a "deep copy" of $options_schools and any changes to $options_schools after this statement will not be reflected in the copy that was made – elitechief21 Jan 21 '14 at 16:42
1

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.

Flosculus
  • 6,880
  • 3
  • 18
  • 42
  • Thanks for your response. As you see from my code, I am trying to make a copy of $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 even outside of the if-else condition. – sunskin Jan 21 '14 at 16:11