PHP | Change strings in an array to uppercase

06 Mar, 2018
You are given an array of strings. You have to change all of the strings present in the given array to uppercase no matter in which case they are currently. Print the resultant array.
Examples:
Input : arr[] = ("geeks", "For", "GEEks")
Output : Array ([0]=>GEEKS [1]=>FOR [2]=>GEEKS)

Input :  arr[] = ("geeks")
Output : Array ([0]=>GEEKS)

To solve this problem one of the basic approach is to iterate over all string of input array and then change them to uppercase on by one and print them. Iterating over array makes a quite use of for loop in program which can be avoided by using some smart methods like array_change_key_case() and array_flip(). What we have to do is just flip the array keys to value and vice-versa after that change the case of new keys of array which actually changes the case of original strings value and then again flip the key and values by array_flip().
Below is the step by step process:
use array_flip() function swap keys with the values present in the array.That is, the keys will now become values and their respective values will be their new keys.use array_change_key_case() function to change case of current keys(original values).use array_flip() function again to flip key and values of array to obtainoriginal array where strings value are in upper case.
use array_flip() function swap keys with the values present in the array.That is, the keys will now become values and their respective values will be their new keys.
use array_change_key_case() function to change case of current keys(original values).
use array_flip() function again to flip key and values of array to obtainoriginal array where strings value are in upper case.
Below is the implementation of above approach in PHP:
<?php  // Program to change strings in an array to upper case  $input = array("Practice", "ON", "GeeKs", "is best");  // print array before conversion of stringprint"Array before string conversion:\n";print_r($input);  // Step 1: flip array key => value$input = array_flip($input);  // Step 2: change case of new keys to upper$input = array_change_key_case($input, CASE_UPPER);  // Step 3: reverse the flip process to // regain strings as value$input = array_flip($input);  // print array after conversion of stringprint"\nArray after string conversion:\n";print_r($input);  ?>
Output :
Array before string conversion:
Array
(
    [0] => Practice
    [1] => ON
    [2] => GeeKs
    [3] => is best
)

Array after string conversion:
Array
(
    [0] => PRACTICE
    [1] => ON
    [2] => GEEKS
    [3] => IS BEST
)

PHP-array
PHP-basics
PHP-string
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to execute PHP code using command line ?
How to Insert Form Data into Database using PHP ?
PHP in_array() Function
How to delete an array element based on key in PHP?
How to convert array to string in PHP ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?