Req #38684 [Fbk->NoF]: How about an array_key_walk_recursive() function?
| From: | php-bugs at lists dot php dot net | Date: | Sun, 29 Jan 2017 04:22:48 +0000 |
| Subject: | Req #38684 [Fbk->NoF]: How about an array_key_walk_recursive() function? | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-207023@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=38684&edit=1
ID: 38684
Updated by: php-bugs@lists.php.net
Reported by: z_rules55 at hotmail dot com
Summary: How about an array_key_walk_recursive() function?
-Status: Feedback
+Status: No Feedback
Type: Feature/Change Request
Package: *General Issues
Operating System: WinXP
PHP Version: 5.1.6
Private report: N
New Comment:
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
Previous Comments:
------------------------------------------------------------------------
[2017-01-20 21:57:16] heiglandreas@php.net
Is this still relevant?
------------------------------------------------------------------------
[2006-09-08 20:03:54] z_rules55 at hotmail dot com
It's been a week since I submitted this, and it's still not assigned to anyone. Do I need
to include more information?
------------------------------------------------------------------------
[2006-09-01 20:45:17] z_rules55 at hotmail dot com
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 this bug report at https://bugs.php.net/bug.php?id=38684&edit=1