#47330 [Bgs]: key return value should be specified

From: Date: Wed, 26 Aug 2009 08:58:00 +0000
Subject: #47330 [Bgs]: key return value should be specified
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-2665@lists.php.net to get a copy of this message
ID: 47330 User updated by: thuejk at gmail dot com Reported By: thuejk at gmail dot com Status: Bogus Bug Type: Documentation problem Operating System: All PHP Version: Irrelevant New Comment: >key() also returns NULL if the array index currently being examined >contains NULL, and therefore gives no useful information as to whether >the end of the array has been passed or not. And anyway, if theis is a case it is an even worse bug than my original report, since the manual states Return Values: Returns the index. It specifically does NOT say anything about returning null if the array value is null. Previous Comments: ------------------------------------------------------------------------ [2009-08-26 08:38:10] thuejk at gmail dot com Umm, sorry, typo, the code <?php $array = Array(0 => null); reset($array); var_dump(current($array)); var_dump(key($array)); ?> of course prints out int(null) int(0) ------------------------------------------------------------------------ [2009-08-26 08:35:51] thuejk at gmail dot com Nope, not true. The code <?php $array = Array(0 => null); reset($array); var_dump(current($array)); var_dump(key($array)); ?> Prints out int(1) int(0) But yes, <?php if (each($array) === false) { $beyond = true; } else { $beyond = false; prev($array); } ?> is the same as <?php $beyond = key($array) === null; ?> Just not as handy. I know about the foreach ($arr as $key => $val) { echo "Got $key => $val\n"; } syntax, but sometimes you need the array iterators, like the following function: <?php //A mergesort which preserves keys. //The naming is like the "a" in build-in function asort() public static function amergesort(Array &$array, $cmp_function='strcmp') { // Arrays of size < 2 require no action. if (count($array) < 2) return; // Split the array in half $halfway = count($array) / 2; $array1 = array_slice($array, 0, $halfway, true); $array2 = array_slice($array, $halfway, sizeof($array), true); // Recurse to sort the two halves self::amergesort($array1, $cmp_function); self::amergesort($array2, $cmp_function); // If all of $array1 is <= all of $array2, just append them. $value1 = end($array1); $key1 = key($array); reset($array1); $value2 = reset($array2); $keys2 = key($array2); if (call_user_func($cmp_function, $value1, $value2) < 1) { $array = $array1; //mergesort will renumber numeric keys foreach ($array2 as $key2 => $value2) { $array[$key2] = $value2; } return; } // Merge the two sorted arrays into a single sorted array $array = array(); while (true) { $value1 = current($array1); $key1 = key($array1); $value2 = current($array2); $key2 = key($array2); if ($key1 === null) { //$array1 is empty - append array 2 do { $array[$key2] = $value2; $value2 = next($array2); $key2 = key($array2); } while ($key2 !== null); break; } else if ($key2 === null) { //$array2 is empty - append array 1 do { $array[$key1] = $value1; $value1 = next($array1); $key1 = key($array1); } while ($key1 !== null); break; } else { //compare elements from $array1, $array2 to see which to append if (call_user_func($cmp_function, $value1, $value2) < 1) { $array[$key1] = $value1; next($array1); } else { $array[$key2] = $value2; next($array2); } } } return; } ?> ------------------------------------------------------------------------ [2009-08-26 07:24:43] torben@php.net key() also returns NULL if the array index currently being examined contains NULL, and therefore gives no useful information as to whether the end of the array has been passed or not. To successfully traverse the array, key() is not the appropriate method. Either of the following will server you better: foreach ($arr as $key => $val) { echo "Got $key => $val\n"; } or reset($arr); while (list($key, $val) = each($arr)) { echo "$key => $val\n"; } ------------------------------------------------------------------------ [2009-02-06 16:47:19] thuejk at gmail dot com Description: ------------ Currently there is no documented way to check whether an array iterator is pointing beyond the end of the array. It would be nice if key($array) could be counted on to return null if that was the case. key does return null in practice, however the documentation does not specify the fact, making me fear that the behaviour could change in the future. See also http://dk.php.net/manual/en/function.key.php ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=47330&edit=1

« previous php.doc.bugs (#2665) next »