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

From: Date: Wed, 26 Aug 2009 16:31:40 +0000
Subject: #47330 [Opn]: key return value should be specified
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-2668@lists.php.net to get a copy of this message
ID: 47330 Updated by: torben@php.net Reported By: thuejk at gmail dot com Status: Open Bug Type: Documentation problem Operating System: All PHP Version: Irrelevant New Comment: Yes, I should have said "defined as NULL" instead of "is NULL". You can define it as NULL but it will be silently converted to the empty string. Previous Comments: ------------------------------------------------------------------------ [2009-08-26 16:26:32] thuejk at gmail dot com Ah, I misunderstood. Null is not a possible array index, hence my confusion. Array indices are always strings or ints. (as I found out when debugging a face of a float being silently truncated to int when used as an array index...) ------------------------------------------------------------------------ [2009-08-26 16:20:34] torben@php.net You're correct that key() does not return NULL if the array value is NULL, but that's not what I said; I said "array index", not "array value". That said, I must apologize as I was also incorrect: key() does not in fact return NULL if the array index is NULL; it returns the empty string. That's what I get for posting when I should have gone to bed instead. Re-opening. ------------------------------------------------------------------------ [2009-08-26 08:58:00] thuejk at gmail dot com >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. ------------------------------------------------------------------------ [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; } ?> ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/47330 -- Edit this bug report at http://bugs.php.net/?id=47330&edit=1

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