Array Functions First Exercise

1. array()
It Returns an array of the parameters. The parameters can be given an index with the => operator
Parameters
- key(Optional): It specifies the key, of type numeric or string. If not set, an integer key is generated, starting at 0
- value(Required) : It specifies the value
Return Values: It returns an array of the parameters.
Syntax
array(key1 => value1, key2 => value2...)
Example
<?php
$array_val= array("val1"=>"computer", "val2"=>"laptop", "val3"=>"mouse");
print_r($array_val);
?>
Output : Array ( [val1] => computer [val2] => laptop [val3] => mouse )
2. array_change_key_case()
It returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
Parameters
- input: The array to work on
- case: Either CASE_UPPER or CASE_LOWER (default)
Return Values: It returns an array with its keys lower or uppercased, or false if the input is not an array.
Syntax
array array_change_key_case ( array $input [, int $case] )
Example
<?php
$inputVal = array("FirSt"=> 100, "SecOnd" => 40);
print_r(array_change_key_case($inputVal, CASE_UPPER));
?>
Output : Array ( [FIRST] => 100 [SECOND] => 40 )
3. array_count_values()
- It returns an array using the values of the input array as keys and their frequency in input as values.
Parameters
- input: The array of values to count
Return Values: It returns an associative array of values from the input as keys and their count as value.
Syntax
array array_count_values ( array $input );
Example
<?php
$input = array("orange", "mango", "banana", "orange" );
print_r(array_count_values($input));
?>
Output : Array ( [orange] => 2 [mango] => 1 [banana => 1 )
4. array_combine()
It creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.
Parameters
- keys: Array of keys to be used
- values: Array of values to be used
Return Values: It returns the combined array, FALSE if the number of elements for each array isn't equal or if the arrays are empty.
Syntax
array array_combine ( array $keys, array $values );
Example
<?php
$abc = array("green","red","yellow");
$bcd = array("mango","apple", "banana");
$cde = array_combine($abc, $bcd);
print_r($cde);
?>
Output : Array([green] => mango [red] => apple [yellow] => banana)
5. array_chunk()
It chunks an array into a size large chunks. The last chunk may contain less than size elements.
Parameters
- input: The array to work on
- size: The size of each chunk
- preserve_keys: When set to TRUE keys will be preserved. Default is FALSE which will reindex the chunk numerically
Return Values: It returns a multidimensional numerically indexed array
Syntax
array array_chunk ( array $input, int $size [, bool $preserve_keys] );
Example
<?php
$input = array("apple","mango", "orange","banana","grapes");
print_r(array_chunk($input, 4));
print_r(array_chunk($input, 2, true));
?>
Output : Array ( [0] => Array ( [0] => apple[1] => mango[2] => orange[3] => banana) [1] => Array ( [0] => grapes) ) Array ( [0] => Array ( [0] => apple[1] => mango) [1] => Array ( [2] => orange[3] => banana) [2] => Array ( [4] => grapes) )
