How to change the case of 2d-associative array

Hi,
I got the following code for changing the case of associative array. Its working fine on single dimensional array. I think it should work on 2d array but when I am trying to process the 2d array, I am getting the following output:

Actual array

Values are in lower case.
Array ( )
Values are in upper case.
Array ( )

The code is given below:

<?php 
function array_change_value_case($input, $ucase) 
{ 
   $case = $ucase; 
   $narray = array(); 
   if (!is_array($input)) 
   { 
      return $narray; 
   } 
   foreach ($input as $key => $value) 
   { 
      if (is_array($value)) 
      { 
         $narray[$key] = array_change_value_case($value, $case); 
         echo "Comes here";
         continue; 
      } 
      $narray[$key] = ($case == CASE_UPPER ? strtoupper($value) :        strtolower($value)); 
   } 
   return $narray; 
} 
//$Color = array('A' => 'Blue', 'B' => 'Green', 'c' => 'Red'); 
$anotherAssocMDArr = array( 
        "first player" => array("name" => 'John', "age" => 'twenty five'),  
        "second player" => array("name" => 'Tim', "age" => 'thirty five') 
); 
echo 'Actual array '.'<BR>'; 
print_r($Color); 
echo '<BR>'.'Values are in lower case.'.'<BR>'; 
$myColor = array_change_value_case($Color,CASE_LOWER); 
print_r($myColor); 
echo '<BR>'.'Values are in upper case.'.'<BR>'; 
$myColor = array_change_value_case($Color,CASE_UPPER); 
print_r($myColor); 
?>

Somebody please guide me how I can modify the above program for 2d arrays.

Zulfi.

This would be a case :roll_eyes: for array_walk_recursive -

function array_change_value_case(&$item, $key, $ucase) 
{
	$item = $ucase == CASE_UPPER ? strtoupper($item) : strtolower($item);
}

$Color = array('A' => 'Blue', 'B' => 'Green', 'c' => 'Red'); 

echo '<pre>';

echo 'Starting array<BR>'; 
print_r($Color); 
echo '<BR>Values are in lower case.<BR>';
array_walk_recursive($Color, 'array_change_value_case', CASE_LOWER);
print_r($Color); 
echo '<BR>Values are in upper case.<BR>';
array_walk_recursive($Color, 'array_change_value_case', CASE_UPPER);
print_r($Color);

$anotherAssocMDArr = array( 
        "first player" => array("name" => 'John', "age" => 'twenty five'),  
        "second player" => array("name" => 'Tim', "age" => 'thirty five') 
);

echo 'Starting array<BR>'; 
print_r($anotherAssocMDArr); 
echo '<BR>Values are in lower case.<BR>';
array_walk_recursive($anotherAssocMDArr, 'array_change_value_case', CASE_LOWER);
print_r($anotherAssocMDArr); 
echo '<BR>Values are in upper case.<BR>';
array_walk_recursive($anotherAssocMDArr, 'array_change_value_case', CASE_UPPER);
print_r($anotherAssocMDArr); 
1 Like

Hi,
Thanks a lot for your response. Sorry, my friend,I can’t find the definition of array_walk_recursive(…), please guide me.

Zulfi.

Where and how did you look?

Goggle search “php array walk recursive”:

https://www.php.net/manual/en/function.array-walk-recursive.php

Edit:

Please also supply a typical 2d-asociative array for testing purposes.