Useful Array Functions In PHP
How to get the number of elements in an array?You can get the number of elements in the array using count() function in PHP.It returns the number of elements as the return value. <?php $colors=array("Red","Green","Blue","Pink","Yellow"); print(count($colors)." elements in the array"); ?> Output: 5 elements in the array |
How to count the frequency of array elements in PHP?Some time you may want to count how many duplicate values in an array and their frequency.In such case you can use array_count_values() function to find out them. Following example explains how to do it. <?php $shop_opend_days_for_month=array("Monday","Tuesday","Wednesday","Friday", "Monday","Wednesday","Thursday","Saturday", "Monday","Wednesday","Friday", "Monday","Tuesday","Wednesday","Saturday"); print_r(array_count_values($shop_opend_days_for_month)); ?> Output: Array ( [Monday] => 4 [Tuesday] => 2 [Wednesday] => 4 [Friday] => 2 [Thursday] => 1 [Saturday] => 2 ) Above function can count string and integer values only. otherwise it throws an warning message 'Can only count STRING and INTEGER values!' |
How to compare the difference of two arrays?Suppose that there are two arrays and there are common elements to both arrays. Then you want to get unique values to the both arrays. In such case you can use array_diff() and array_merge() functions in php. Following example shows how to do it.<?php $colors1=array("Red","Green","Blue","Black","White","Orange"); $colors2=array("Red","Green","Black","Blue","Pink","White","Silver"); print_r(array_merge(array_diff($colors1,$colors2),array_diff($colors2,$colors1))); ?> Output: Array ( [0] => Orange [1] => Pink [2] => Silver ) array_diff() function compares array1 against array2 and returns the difference as an array. array_merge() function merges the elements of one or more arrays together and returns an array with elements of input arrays. |
How to combine two arrays?Sometimes you may want to append one or more arrays in to one array. In such cases you can use array_merge() function to do it. Following example shows how to do that.<?php $colors1=array("Red","Green","Blue"); $colors2=array("Pink","White","Silver"); $colors3=array("Gold","Black","Orange"); print_r(array_merge($colors1,$colors2,$colors3)); ?> Output: Array ( [0] => Red[1] => Green[2] => Blue[3] => Pink[4] => White[5] => Silver[6] => Gold[7] => Black[8] => Orange ) |
How to reverse the order of the values of an array?You can reverse the order of the array elements using array_reverse() function in PHP. Following example shows how to do it.<?php $array=array("First","Second","Third","Forth","Fifth"); print_r(array_reverse($array)); ?> Output: Array ( [0] => Fifth [1] => Forth [2] => Third [3] => Second [4] => First ) |
How to create an array with default values for it's elements?Sometimes you may want to create an array which has default value to its elements. In such case you can use array_fill() function. Following example shows how to do it.<?php $colors = array_fill(0, 6,'White'); print_r($colors); ?> Output: Array ( [0] => White [1] => White [2] => White [3] => White [4] => White [5] => White ) |
How to add elements to the end of an array?In some case you may want to add one or more elements to the end of an array. using php array_push() function you can do it. Following example shows how to do it.<?php $days = array("Sunday","Monday","Tuesday","Wednesday"); array_push($days,"Thursday","Friday","Saturday"); print_r($days); ?> Output: Array ( [0] => Sunday [1] => Monday [2] => Tuesday [3] => Wednesday [4] => Thursday [5] => Friday [6] => Saturday ) |
How to add elements to the beginning of an array?In some case you may want to add one or more elements to the front of an array. using php array_unshift() function you can do it. Following example shows how to do it.<?php $numbers = array(4,5,6,7); array_unshift($numbers,1,2,3); print_r($numbers); ?> Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 ) |
How to calculate the sum of an array which contains integer or floating point values?Sometimes you may want to calculate the sum of an array elements using array_sum() function. when you are using integer or floating point values in the array. For an example there is an array containing marks of a student according to the subjects. Then you can calculate the total marks and the total and average marks in convenient way instead of adding them one by one. Following example shows how to do this.<?php $marks = array('Maths'=>84,'Science'=>92,'English'=>64,'History'=>70,'Music'=>96); print("Total marks for ".count($marks)." subjects = ".array_sum($marks)."<br>"); print("Average marks = ".array_sum($marks)/count($marks)); ?> Output: Total marks for 5 subjects = 406 Average marks = 81.2 * array_sum() function works with only integer and floating point numbers. |
Try It!
JavaScript Validation