PHP 8.5.0 Alpha 4 available for testing

Voting

: max(nine, seven)?
(Example: nine)

The Note You're Voting On

turabgarip at gmail dot com
4 years ago
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.

<< Back to user notes page

To Top