From: z_rules55 at hotmail dot com Operating system: WinXP PHP version: 5.1.6 PHP Bug Type: Feature/Change Request Bug description: How about an array_key_walk_recursive() function?
Description: ------------ While working recently on a project that involves JSON, I saw that it might be necessary to make sure that a an array's keys are in utf-8 encoding for when I pass it to the JSON encoder, since JSON expects stuff to be in utf-8. One possible way to do this with PHP's existing array functions is with array_combine(), and converting all the elements in the keys array to utf-8 before combining it with the elements array, but this seems like it could become more tedious with nested arrays. I tried using foreach($array as &$key => $val) to convert the key's encoding in each iteration, but got an error saying "Fatal error: Key element cannot be a reference". So I wrote my own function (included below). Why isn't there an array_key_walk_recursive() function, or something of the kind? A function that would let you apply a callback to all keys in an array. Or, let you use foreach with a referenced key as I mentioned. Reproduce code: --------------- <?php function array_convert_key_encoding($array) { $encoded_array = array(); foreach($array as $key => $val) { if(is_string($key) && is_array($val)) { $encoded_array[utf8_encode($key)] = array_convert_key_encoding($val); } else if(is_string($key)) { $encoded_array[utf8_encode($key)] = $val; } else { $encoded_array[$key] = $val; } } return $encoded_array; } ?> -- Edit bug report at http://bugs.php.net/?id=38684&edit=1 -- Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=38684&r=trysnapshot44 Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=38684&r=trysnapshot52 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=38684&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=38684&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=38684&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=38684&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=38684&r=needscript Try newer version: http://bugs.php.net/fix.php?id=38684&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=38684&r=support Expected behavior: http://bugs.php.net/fix.php?id=38684&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=38684&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=38684&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=38684&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=38684&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=38684&r=dst IIS Stability: http://bugs.php.net/fix.php?id=38684&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=38684&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=38684&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=38684&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=38684&r=mysqlcfg