note 110558 added to class.arrayobject

From: Date: Tue, 06 Nov 2012 22:28:35 +0000
Subject: note 110558 added to class.arrayobject
Groups: php.notes 
Request: Send a blank email to php-notes+get-191786@lists.php.net to get a copy of this message
Long story short b/c arrays by default are passed by value, if you pass an array to a function, the function works on a copy of the array while the original array remains unaltered by the function. You may cause a change to the array to be reflected in the original array by having the function return the altered array and assign it to the variable for the original array, as follows: function my_array_modify($data) { $data['b'] = 2; return $data; } $regularArray = array(); $regularArray['a'] = 1; $regularArray = my_array_modify($regularArray); var_dump($regularArray['b']); // 2 Or, you may explicitly pass the array by reference in which case there is no need for the function to return the array since the change will have effected the original array, as follows: function my_arrayref_modify(&$data) { $data['bb'] = 22; } my_arrayref_modify($regularArray); var_dump($regularArray['bb']); // 22 ---- Server IP: 69.147.83.199 Probable Submitter: 216.175.76.243 ---- Manual Page -- http://www.php.net/manual/en/class.arrayobject.php Edit -- https://master.php.net/note/edit/110558 Del: integrated -- https://master.php.net/note/delete/110558/integrated Del: useless -- https://master.php.net/note/delete/110558/useless Del: bad code -- https://master.php.net/note/delete/110558/bad+code Del: spam -- https://master.php.net/note/delete/110558/spam Del: non-english -- https://master.php.net/note/delete/110558/non-english Del: in docs -- https://master.php.net/note/delete/110558/in+docs Del: other reasons-- https://master.php.net/note/delete/110558 Reject -- https://master.php.net/note/reject/110558 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#191786) next »