note 126699 added to language.references.unset

From: Date: Mon, 20 Dec 2021 00:00:29 +0000
Subject: note 126699 added to language.references.unset
Groups: php.notes 
Request: Send a blank email to php-notes+get-217396@lists.php.net to get a copy of this message
A little quirk on unset() when using references that may help someone. If you want to delete the element of a reference to an array, you need to have the reference point to the parent of the key that you want to delete. <?php $arr = array('foo' => array('foo_sub1' => 'hey', 'foo_sub2' => 'you'), 'bar' => array('bar_sub1' => 'good', 'bar_sub2' => 'bye')); $parref = &$arr['foo']; $childref = &$parref['foo_sub1']; unset($childref); // this will simply unset the reference to child unset($parref['foo_sub1']); // this will actually unset the data in $arr; $parref['foo_sub1'] = NULL; // this will set the element to NULL, but not delete it. If you run it after unset(), it add the key back and set it to NULL ?> This is nice to use for passing something dynamically to a function by reference without copying the entire array to the function, but you want to do some maintenance on the array. ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 107.77.212.184) ---- Manual Page -- https://php.net/manual/en/language.references.unset.php Edit -- https://main.php.net/note/edit/126699 Del: integrated -- https://main.php.net/note/delete/126699/integrated Del: useless -- https://main.php.net/note/delete/126699/useless Del: bad code -- https://main.php.net/note/delete/126699/bad+code Del: spam -- https://main.php.net/note/delete/126699/spam Del: non-english -- https://main.php.net/note/delete/126699/non-english Del: in docs -- https://main.php.net/note/delete/126699/in+docs Del: other reasons-- https://main.php.net/note/delete/126699 Reject -- https://main.php.net/note/reject/126699 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217396) next »