Skip to content

Use Of Array_change_key_case() And Array_chunk() Functions In PHP

In this article, you will be learning about the use of array_change_key_case() and array_chunk() functions in php.

The function array_change_key_case() is used to change the keys of an array to either lowercase or uppercase.

The function array_chunk() is used to divide or split an array into chunks(divisions).

The description of the above two function is given below in detail. But you may also join our advanced In-Class and Online php training in India to improve your php programming skill set.

ARRAY_CHANGE_KEY_CASE() FUNCTION

Syntax : array_change_key_case($arr, $case);

Two parameters are used in this function which are described below.

  • $arr- It represents the array whose keys are to be uppercased or lowercased.
  • $case- It can have only two default values. Uppercase or Lowercase. No other values can be included.

Result/ Return Value : On the successful execution of the function, an array with the keys uppercased or lowercased will be returned. False will be returned if the values other than array are used.

php-keycase-1024x359

Example : An example of array_change_key_case() function is shown below. 

php-keycaseoutput-1024x223

Output : You will get the following output after running the above php script. Here, we have taken a variable ‘$a’ which stores the array keys ‘hello’ and ‘world’ in the lowercase. We have then used the array_change_key_case() function to change the keys to the uppercase. Clearly, the output will show the keys in the uppercase HELLO and WORLD as shown in the image below.

ARRAY_CHUNK() FUNCTION

Syntax : array_chunk($arr, $size, $preserve_keys);

Three parameters are used in this function which are described below.

  • $arr- It represents the array of elements to be divided into chunks.
  • $size- It represents an integer value corresponding to the divisions being made.
  • $preserve_keys- This is an additional parameter which can have either of the two values. (a) TRUE- used to preserve the keys.(b) FALSE- it is the default value used to reindex the divisions being made.

Result/ Return Value : The successful execution of the function will return a multi-dimensional divisions of array starting at zero. Null will be returned if the size is less than 1.

Errors : You will get an E_WARNING if the size is declared as 1 in the function.

php-chunk-1024x440

Example : We have shown below an example of array_chunk() function. 

Output : The following output will be generated after running the above php script. Here, we have used a variable $a having array values 10, 20, 30, 40, ‘w3training’ and ‘school’. Before using the array_chunk() function, the output will show an undivided array of elements having all the six values in a single group. After using the array_chunk() function and declaring size of the chunk as 4, the output array will be splitted into a group of four elements 10, 20, 30 and 40. The remaining two elements ‘w3training’ and ‘school’  will be seen in the next chunk.

php-chunkoutput-1024x540

We have tried to give you a precise information about the use of array_change_key_case() and array_chunk() functions in php. Hope you liked this article. For further updates and information, keep visiting our php blogs.

Facebook
Twitter
LinkedIn
Pinterest