PHP Change strings in an array to uppercase

PHP

Examples:
Input: arr [] = ("engineer", "For", "GEEks") Output: Array ([0] = > GEEKS [1] = > FOR [ 2] = > GEEKS) Input: arr [] = ("engineer") Output: Array ([0] = > GEEKS) 
To solve this problem, one of the main approaches is to loop over the entire line of the input array and then change them to uppercase and print them. Iterating over an array allows for a for loop in the program, which can be avoided with some smart methods like array_change_key_case() and array_flip() . What we need to do is just flip the keys of the array to a value and vice versa after that change the case of the new keys of the array, which will actually change the case of the original string value, and then flip the key and values ​​again with array_flip().Below is a step-by-step process:
  • use the array_flip() function for exchange of keys with the values ​​represented in the array. 
    That is, the keys will now become values, and their corresponding values ​​will be their new keys.
  • use array_change_key_case() function, to change the case of the current keys (original values).
  • again use array_flip() function to toggle key and array values ​​for getting
    the original array, where the string values ​​are in uppercase.
  • Below is the implementation of the above approach in PHP:  
    // Program for changing lines in an array in the upper th case $input = array ( "Practice" , "ON" , "GeeKs" , "is best" );  
    // print the array before converting the string print "Array before string conversion:" ; print_r ( $input );  
    // Step 1: reverse array key = > value $input = array_flip ( $input );  
    // Step 2: change the case of new keys to uppercase $input = array_change_key_case ( $input , CASE_UPPER);  
    // Step 3: flip the flipping process to
    // restore strings as value $input = array_flip ( $input );  
    // print the array after transforming the string print "Array after string conversion:" ; 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) 




    Get Solution for free from DataCamp guru