Converting string elements of an array to upper and lower case, maintaining names of keys I'm just going through some array exercises in PHP on w3resource. I have just completed:

Sample array :
$Color = array('A' =&gt; 'Blue', 'B' =&gt; 'Green', 'c' =&gt; 'Red');
Expected Output :
Values are in lower case:
Array ( [A] =&gt; blue [B] =&gt; green [c] =&gt; red )
Values are in upper case:
Array ( [A] =&gt; BLUE [B] =&gt; GREEN [c] =&gt; RED )

Here is my answer:
&lt;?php
    echo &quot;&lt;pre&gt;&quot;;
    
    $Color = array('A' =&gt; 'Blue', 'B' =&gt; 'Green', 'c' =&gt; 'Red');
    
    echo &quot;Sample array:&lt;br&gt;&quot;;
    print_r($Color);
    
    function toLower($value)
    {
        return strtolower($value);   
    }
    
    function toUpper($value)
    {
        return strtoupper($value);
    }
    
    echo &quot;&lt;br&gt;Values are in lower case:&lt;br&gt;&quot;;
    print_r(array_map(&quot;toLower&quot;, $Color));

    echo &quot;&lt;br&gt;Values are in upper case:&lt;br&gt;&quot;;
    print_r(array_map(&quot;toUpper&quot;, $Color));
    
    echo &quot;&lt;/pre&gt;&quot;;
?&gt;

And its output:
Sample array:
Array
(
    [A] =&gt; Blue
    [B] =&gt; Green
    [c] =&gt; Red
)

Values are in lower case:
Array
(
    [A] =&gt; blue
    [B] =&gt; green
    [c] =&gt; red
)

Values are in upper case:
Array
(
    [A] =&gt; BLUE
    [B] =&gt; GREEN
    [c] =&gt; RED
)

Some questions:

Originally I thought about using implode to convert the elements to a string and then set the case of the entire string before explodeing back to an array; but of course this loses the names of the keys (A, B, c). Is there any possible way to somehow use implode and explode to get around this (convert to string while somehow keeping key names in memory)?

As the questions call explicitly for a function rather than a program, should this sort of operation be done with one function rather than the two which I have implemented here?

Is this an appropriate usage of array_map?

w3resource's sample answer makes use of statements involving is_array. Is it good practise to implement such guard conditions in my own code at this point?

Is there anything about my code which looks strange to you, or could easily be improved?
Is there any possible way to somehow use implode and explode to get around this (convert to string while somehow keeping key names in memory)?


You can get all the keys using array_keys then combine them with array_combine.

$upperCase = array_combine(array_keys($Color), explode(',', strtoupper(implode(',', $Color))));
print_r($upperCase);


But why do all these efforts? Your solution is perfect, you could not do better.


  should this sort of operation be done with one function rather than the two


If you have no need to do anything else but convert the text in lowercase/uppercase, then just call 

print_r(array_map('strtoupper', $Color));



  Is it good practise to implement such guard conditions in my own code at this point?


It is advisable when you do not know the data you're working. It is not in this case.