php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #38684 How about an array_key_walk_recursive() function?
Submitted: 2006-09-01 20:45 UTC Modified: 2017-01-29 04:22 UTC
From: z_rules55 at hotmail dot com Assigned:
Status: No Feedback Package: *General Issues
PHP Version: 5.1.6 OS: WinXP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: z_rules55 at hotmail dot com
New email:
PHP Version: OS:

 

 [2006-09-01 20:45 UTC] 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;
}
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-09-08 20:03 UTC] 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?
 [2017-01-20 21:57 UTC] heiglandreas@php.net
-Status: Open +Status: Feedback -Package: Feature/Change Request +Package: *General Issues
 [2017-01-20 21:57 UTC] heiglandreas@php.net
Is this still relevant?
 [2017-01-29 04:22 UTC] php-bugs at lists dot php dot net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 06:01:32 2024 UTC