php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52917 Function to change case-key recursively
Submitted: 2010-09-23 20:53 UTC Modified: 2010-09-24 17:38 UTC
From: luis dot pessoa67 at ibest dot com dot br Assigned:
Status: Duplicate Package: Arrays related
PHP Version: 5.3.3 OS: Any
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: luis dot pessoa67 at ibest dot com dot br
New email:
PHP Version: OS:

 

 [2010-09-23 20:53 UTC] luis dot pessoa67 at ibest dot com dot br
Description:
------------
Hi, I suggest to create in the next php versions a function similar to 
array_change_key_case() but doing it recursevely. Bellow I put some example to doing that and the results.

regards, Luis

Test script:
---------------
<?php

    function arrKey2Lower(&$arrVals) {
        foreach( $arrVals as $key => $item ) {
            $key2 = strtolower($key);
            if ( $key2 != $key) {
                unset($arrVals[$key]);
                $arrVals[$key2]=$item;
                $key=$key2;
            }
            
            if ( is_array($item) ) { arrKey2Lower($arrValores[$key]); }
        }
    }
    
    $arr = array('ID' => 1, 'NAME'=> 'Luis', 'Contact' => array('PHONE' => '3010-7148', 'E-MAIL' => 'luis@net.com') );
    arrKey2Lower($arr);
    var_dump($arr);
    
    
?>

Expected result:
----------------
array(3) {
  ["id"]=>
  int(1)
  ["name"]=>
  string(4) "Luis"
  ["contact"]=>
  array(2) {
    ["phone"]=>
    string(9) "3010-7148"
    ["e-mail"]=>
    string(12) "luis@net.com"
  }
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-09-23 21:21 UTC] kalle@php.net
-Package: Unknown/Other Function +Package: Arrays related
 [2010-09-23 21:21 UTC] kalle@php.net
It might be worth adding a recursive version or flag to the array_change_key_case() function
 [2010-09-24 10:27 UTC] aharvey@php.net
-Status: Open +Status: Duplicate
 [2010-09-24 10:27 UTC] aharvey@php.net
Duplicate of request #31058.
 [2010-09-24 17:38 UTC] luis dot pessoa67 at ibest dot com dot br
- Yes Kalle! this is the best way. Tks.

- Tks for the warning aharvey. I saw the 31058 request code and get improved mine to this:

    function ack_r2(&$array, $case) 
    {
      $array = array_change_key_case($array, $case);
      foreach ($array as $key => $value) {
        if ( is_array($value) ) {
            ack_r2($array[$key], $case);
        }
      }
    }
 [2010-09-24 17:46 UTC] luis dot pessoa67 at ibest dot com dot br
Improving more:

    function ack_r3(&$array, $case=CASE_LOWER, $flag_rec=false) 
    {
      $array = array_change_key_case($array, $case);
      if ( $flag_rec ) {
        foreach ($array as $key => $value) {
            if ( is_array($value) ) {
                ack_r3($array[$key], $case, true);
            }
        }
      }
    }
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Fri Nov 16 11:01:25 2018 UTC