note 126158 added to function.reset

From: Date: Wed, 09 Jun 2021 14:32:40 +0000
Subject: note 126158 added to function.reset
Groups: php.notes 
Request: Send a blank email to php-notes+get-216506@lists.php.net to get a copy of this message
Since reset() returns the first "value" of the array beside resetting its internal pointer; it will return different results when it is combined with key() or used separately. Like; <?php $products = array( 'biscuits' => array('biscuit1' => 'cobis', 'biscuit2' => 'probis'), 'chocolates' => array('coco1' => 'cococ', 'coco2' => 'prococ'), ); echo key(reset($products['biscuits'])); // Fatal error reset($products['biscuits']); echo key($products['biscuits']); // Will print 'biscuit1' ?> This is perfectly normal because in the first method, reset() returned the first "value" of the 'biscuits' element which is to be "cbosi". So key(string) will cause a fatal error. While in the second method you just reset the array and didn't use a returning value; instead you reset the pointer and than extracted the first key of an array. If your array has more dimensions, it won't probably cause a fatal error but you will get different results when you combine reset() and key() or use them consecutively. ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 212.154.90.50) ---- Manual Page -- https://php.net/manual/en/function.reset.php Edit -- https://main.php.net/note/edit/126158 Del: integrated -- https://main.php.net/note/delete/126158/integrated Del: useless -- https://main.php.net/note/delete/126158/useless Del: bad code -- https://main.php.net/note/delete/126158/bad+code Del: spam -- https://main.php.net/note/delete/126158/spam Del: non-english -- https://main.php.net/note/delete/126158/non-english Del: in docs -- https://main.php.net/note/delete/126158/in+docs Del: other reasons-- https://main.php.net/note/delete/126158 Reject -- https://main.php.net/note/reject/126158 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#216506) next »