w3resource

PHP Array Exercises : Lower-case and upper-case, all elements in an array


36. Convert Array Elements to Lower and Upper Case

Write a PHP script to lower-case and upper-case, all elements in an array.

Sample Solution:

PHP Code:

<?php
// Define an array with string values
$colors = array( "Red", "Green", "Black", "White"); 

// Display the original array using print_r
print_r($colors);

// Use array_map and strtolower to create an array with lowercase versions of each string
$lower_colors = array_map('strtolower', $colors);

// Display the array containing lowercase versions
print_r($lower_colors);

// Use array_map and strtoupper to create an array with uppercase versions of each string
$upper_colors = array_map('strtoupper', $colors);

// Display the array containing uppercase versions
print_r($upper_colors);

?>

Output:

Array                                                       
(                                                           
    [0] => Red                                              
    [1] => Green                                            
    [2] => Black                                            
    [3] => White                                            
)                                                           
Array                                                       
(                                                           
    [0] => red                                              
    [1] => green                                            
    [2] => black                                            
    [3] => white                                            
)                                                           
Array                                                       
(                                                           
    [0] => RED                                              
    [1] => GREEN                                            
    [2] => BLACK                                            
    [3] => WHITE                                            
)

Flowchart:

Flowchart: PHP - Lower-case and upper-case, all elements in an array

For more Practice: Solve these Related Problems:

  • Write a PHP script to convert all array elements to lowercase using array_map() and display the modified array.
  • Write a PHP function to convert array elements to uppercase and then merge the original and converted arrays into a single result.
  • Write a PHP program to toggle the case of each element in an array and output both versions side by side.
  • Write a PHP script to use array_walk() to change the case of each array element based on a parameter indicating 'upper' or 'lower'.

Go to:


PREV : Trim All Array Elements Using array_walk.
NEXT : Count Occurrences of a Specific Value in Array.

PHP Code Editor:



Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.