El Retardo
El Retardo

Reputation: 21

PHP array being passed by copy

I am trying to permanently reset the average mark of each student in the array but with no luck. I am passing the array containing all the students to my function using the & operator so that it is passed by reference. I run my function that resets the average mark and print out the results in a table and indicates that the average mark has been reset inside the method. I then call var_dump() on the array outside of the method to check if the changes are still in effect. The output of var_dump() show the average mark unchanged :'(. Any help would be greatly appreciated.

ID          Name    Surname Address Telephone   Average
123345(M)   Roger   Fenech  USA     21212121    0
835392(M)   Chris   Tabone  UK      21456780    0
693648(M)   Thomas  Grech   Spain   21854595    0
483582(M)   Michael Abela   Turkey  24872639    0
742652(M)   Stan    Galea   France  21951357    0

var_dump():

array(5) { [0]=> array(6) { ["ID"]=> string(9) "123345(M)" ["Name"]=> string(5) "Roger" ["Surname"]=> string(6) "Fenech" ["Address"]=> string(3) "USA" ["Telephone"]=> int(21212121) ["Average"]=> int(100) } [1]=> array(6) { ["ID"]=> string(9) "835392(M)" ["Name"]=> string(5) "Chris" ["Surname"]=> string(6) "Tabone" ["Address"]=> string(2) "UK" ["Telephone"]=> int(21456780) ["Average"]=> int(78) } [2]=> array(6) { ["ID"]=> string(9) "693648(M)" ["Name"]=> string(6) "Thomas" ["Surname"]=> string(5) "Grech" ["Address"]=> string(5) "Spain" ["Telephone"]=> int(21854595) ["Average"]=> int(60) } [3]=> array(6) { ["ID"]=> string(9) "483582(M)" ["Name"]=> string(7) "Michael" ["Surname"]=> string(5) "Abela" ["Address"]=> string(6) "Turkey" ["Telephone"]=> int(24872639) ["Average"]=> int(88) } [4]=> array(6) { ["ID"]=> string(9) "742652(M)" ["Name"]=> string(4) "Stan" ["Surname"]=> string(5) "Galea" ["Address"]=> string(6) "France" ["Telephone"]=> int(21951357) ["Average"]=> int(76) } }

Code:

<html>
<head>
    <title>PHP Test</title>
</head>
<style type="text/css">
table{text-align: center}
</style>
<body>

    <?php 

    $students = array
    (
        array(
            'ID' => '123345(M)', 
            'Name' => 'Roger',
            'Surname' => 'Fenech',
            'Address' => 'USA',
            'Telephone' => 21212121,
            'Average' => 100
            ),
        array(
            'ID' => '835392(M)', 
            'Name' => 'Chris',
            'Surname' => 'Tabone',
            'Address' => 'UK',
            'Telephone' => 21456780,
            'Average' => 78
            ),
        array(
            'ID' => '693648(M)', 
            'Name' => 'Thomas',
            'Surname' => 'Grech',
            'Address' => 'Spain',
            'Telephone' => 21854595,
            'Average' => 60
            ),
        array(
            'ID' => '483582(M)', 
            'Name' => 'Michael',
            'Surname' => 'Abela',
            'Address' => 'Turkey',
            'Telephone' => 24872639,
            'Average' => 88
            ),
        array(
            'ID' => '742652(M)', 
            'Name' => 'Stan',
            'Surname' => 'Galea',
            'Address' => 'France',
            'Telephone' => 21951357,
            'Average' => 76
            )
        );

    function resetAverage(&$students){
        echo "<table>
        <tr>
        <th>ID</th> 
        <th>Name</th>
        <th>Surname</th>
        <th>Address</th>
        <th>Telephone</th>
        <th>Average</th>
        </tr>"                  ;
        foreach ($students as $student) {
            echo "<tr>";
            unset($student["Average"]);
            $student["Average"] = 0;
            foreach ($student as $key => $value) {
                echo "<td>" . $value . "</td>";
            }   
            echo '</tr>';
        }
        echo "</table>";
    }

    resetAverage($students);

    echo "<pre>" . var_dump($students) . "</pre>";
    ?>
</body>
</html>

Upvotes: 0

Views: 45

Answers (2)

Cristik
Cristik

Reputation: 32814

You need to also pass by reference the $student array: foreach ($students as &$student), otherwise each student array will be copied when iterated.

Upvotes: 1

Barmar
Barmar

Reputation: 781096

You also need to use a reference in the foreach loop:

foreach ($students as &$student) {

Otherwise $student is a copy of the array element, so unset($student["Average"]) only affects the copy, not the original array.

Upvotes: 3

Related Questions