4 life savings php array functions

4 life savings php array function for web developers

Array is one of the most frequently used term of every programming language. In the web scripting language like php or javascripts – we can not think multiple data manipulation without an array. In this tutorial, I am going to show four important array functions of the PHP scripts. Lets get started-

1. array_change_key_case()

2. array_chunk()

3. array_combine()

4. array_count_values()

5. sizeof()

6. Count()

1. array_change_key_case():

This functions takes two parameters as an input. First parameter contains an array and second parameter contains a keyword. There are two extra  options available to this function. such as – CASE_UPPER and CASE_LOWER.

This function converts an array index in to lowercase/uppercase based on the keyword settings.

– Example of array_change_key_case():

$input = array (
    "first" =>1,
    "second" =>2,
    "third" =>3,
    "fourth" =>4

2. array_chunk():

This function takes an array as input, and it has additional 2 parameters. First parameters is an array, second parameter is size of chunk and third parameter is a Boolean value which is optional. If third parameter set as true then its chucked array by maintained its indexed value. Otherwise chucked array index always starts from zero(0).

– Example of array_chunk() :

$input_array = array ('a','b','c','d','e');

3. array_combine():

* This function takes 2 arrays as input. First array element considered as key and second array element considered as value. It’s return an array. It shows an error if any element is empty or number of elements does not matched.

– Example of array_combine() :

$a = array("red","green","yellow");
$b = array("mango","apple","orange");
$c = array_combine($a,$b);

4. array_count_values():

This function takes an array as input. It show the number of occurrence of a value in an array. It return an array.

– Example of array_count_values() :

$input_array = array (1,"hello",1,"world","hello");
$ouput_array = array_count_values($input_array);

5. sizeof():

This function takes an array as input. The absolute value of number. If the argument number is of type float, the return type is also float, otherwise it is integer (as float usually has a bigger value range than integer). It return integer value.

– Example of sizeof() function:

$input_array = array ("dog","cat","bird","man","cow");
$array_size = sizeof($input_array);            
echo "Array size:".$array_size;

6. count() function:

This function takes an array as input. The number of elements in var. If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is null, 0 will be returned. count may return 0 for a variable that isn’t set, but it may also return 0 for a variable that has been initialized with an empty array. Use isset to test if a variable is set. It return integer value.

– Example of count() function:

$input_array = array ("pink","red","yellow","blue","green");
$array_size = count($input_array);            
echo "Array size:".$array_size;

That’s all for today.  Was this information useful? What other tips would you like to read about in the future? Share your comments, feedback and experiences with us by commenting below!

Total 0 Votes

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Comment

Back To Top