PHP | Change strings in an array to uppercase
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 (=>GEEKS =>FOR =>GEEKS) Input : arr = ("geeks") Output : Array (=>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 one 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 obtain original array where strings value are in upper case.
Below is the implementation of above approach in PHP:
Array before string conversion: Array (  => Practice  => ON  => GeeKs  => is best ) Array after string conversion: Array (  => PRACTICE  => ON  => GEEKS  => IS BEST )
Please Login to comment...