Converting Array Values To Uppercase
In this post i am going to explain how to convert all the values of a php array into uppercase. This is important if your arrays are used in a case sensitive matching or validation. This can be achieved using 2 methods as shown below.
Method 1 – Looping through the array and converting to uppercase
In this method we have to loop through the array and convert each array value to uppercase. Then we need to add the converted value to a new array.
$sample_array = array("mark","steve","adam"); $converted_array = array(); foreach($sample_array as $key=>$value){ $uppercase_value = strtoupper($value); array_push($uppercase_value,$converted_array); }
Method 2 – Using php array_map function
Using this method we can convert array values using a single line of code. This function can be used to apply single function on every array element without looping through the array manually. This method is recommended for converting array values.
$sample_array = array("mark","steve","adam"); $converted_array = array_map("strtoupper", $sample_array);
August 30th, 2011 at 3:45 pm
Great Post
September 2nd, 2011 at 10:42 pm
Very nice post. I just stumbled upon your weblog and wanted to say that I have truly enjoyed browsing your blog posts. After all I will be subscribing to your feed and I hope you write again very soon!
September 12th, 2011 at 7:16 pm
I love reading these articles beacsue they’re short but informative.