PHP Array Functions

array_change_key_case

PHP Array Function array_change_key_case is an inbuilt PHP function used to change the case of all keys in the array.

With the help of this inbuilt function, you can convert your PHP array values to lowercase, uppercase.

Syntax:

<?php 
         array_change_key_case ( array $array [, int $case = CASE_LOWER ] ) : array
 ?>

Parameter:

This function accepts two parameters as shown in the above syntax. The parameters are described below:

  • array: This parameter represents the array that is needed to change the case.
  • case: This parameter represents in which case you have to convert your values. Either UPPER_CASE or LOWER_CASE. LOWER_CASE is a default value.

Return Value:

This function returns an array with its a key in lower or uppercase. It depends on the parameter you had passed. It will return FALSE if the first parameter is not an array.

Example:

<?php
     $input_array = array("pHp" => 1, "jAVa" => 2);
     print_r(array_change_key_case($input_array, CASE_UPPER));
?>

The result of the above code is

Array
(
    [PHP] => 1
    [JAVA] => 4
)

In the input_array variable, we pass two keys with mixed cases lowe and uppercase. When we call the array_change_key_case function with the second parameter CASE_UPPER to change the case in it. Our key change in the upper case.

array_chunk

PHP inbuilt function array_chunk is used to split an array into chunks. It split an array into chunks depending upon the parameters passed in the function.

The last element may contain less than the size element.

Syntax:

<?php
array_chunk ( array $array , int $size [, bool $preserve_keys = FALSE ] ) : array
?>

Parameter:

This function accepts three parameters as shown in the above syntax. The parameters are described below:

  • array: The first parameter function accept is an array to work on.
  • size: The second parameter function accept is size. An integer number to split an array into chunks or you can say the size of chunks.
  • preserve_keys: When third parameter is set to TRUE keys will be preserved. Default is FALSE which will reindex the chunk numerically.

Return Value:

This function returns a multidimensional numerically indexed array, starting with zero, with each dimension containing SIZE elements.

Example:

<?php
$input_array = array('PHP', 'JAVA', 'NET', 'RUBY', 'PYTHON');
print_r(array_chunk($input_array, 2));
?>

In the above example, we had passed 2 parameters. Last parameter preserve_keys will take default value FALSE. Here is the result,

Array
(
    [0] => Array
        (
            [0] => PHP
            [1] => JAVA
        )

    [1] => Array
        (
            [0] => NET
            [1] => RUBY
        )

    [2] => Array
        (
            [0] => PYTHON
        )

)

As you can see in the above result array is reindex the chunk numerically. Now if you don’t want to reindex the chunk and want to keep the same index pass the third parameter as TRUE.

Here is an example.

<?php
$input_array = array('PHP', 'JAVA', 'NET', 'RUBY', 'PYTHON');
print_r(array_chunk($input_array, 2, true));
?>
Array
(
    [0] => Array
        (
            [0] => PHP
            [1] => JAVA
        )

    [1] => Array
        (
            [2] => NET
            [3] => RUBY
        )

    [2] => Array
        (
            [4] => PYTHON
        )

)

Reference: http://php.net/manual/en/function.array-chunk.php

LIKE THIS ARTICLE?

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Leave a Comment