1
\$\begingroup\$

I'm just going through some array exercises in PHP on w3resource. I have just completed:

Sample array :

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

Expected Output :

Values are in lower case:

Array ( [A] => blue [B] => green [c] => red )

Values are in upper case:

Array ( [A] => BLUE [B] => GREEN [c] => RED )

Here is my answer:

<?php
    echo "<pre>";

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

    echo "Sample array:<br>";
    print_r($Color);

    function toLower($value)
    {
        return strtolower($value);   
    }

    function toUpper($value)
    {
        return strtoupper($value);
    }

    echo "<br>Values are in lower case:<br>";
    print_r(array_map("toLower", $Color));

    echo "<br>Values are in upper case:<br>";
    print_r(array_map("toUpper", $Color));

    echo "</pre>";
?>

And its output:

Sample array:
Array
(
    [A] => Blue
    [B] => Green
    [c] => Red
)

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

Values are in upper case:
Array
(
    [A] => BLUE
    [B] => GREEN
    [c] => 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?

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.