CodingBison

The next set of array-based functions features functions that focus on entire array instead of individual elements. We present them in two sets.

The first set consists of the following functions:

 $sum_of_elements = array_sum($array);
 $merged_array    = array_merge($array1, $array2, ...);
 $diff_array      = array_diff($array1, $array2, ...);
 $unique_array    = array_unique($array);
 $reversed_array  = array_reverse($array);

Let us describe these functions.

array_sum() returns the sum of all the elements present in an array. array_merge() merges two or more arrays into one single array. array_diff() extracts elements that are not common from two or more arrays. array_unique() returns a new array by removing duplicate entries. array_reverse(), as the name suggests, returns a new array that reverses the ordering of the elements present in the passed array.

We use a simple program (provided below) to illustrate the usage of these functions.

 <?php
 $array_tens = array(10, 20, 30);
 echo "The array is: <br>";
 print_r($array_tens);
 $sum = array_sum($array_tens);
 echo "<br>The sum of array is $sum <br>";

 $array_fives = array(15, 25, 5); 
 $array_new = array_merge($array_tens, $array_fives);
 sort($array_new);
 echo "<br>After merging, the new array is: <br>";
 print_r($array_new);

 $array_userid1 = array("jfulton", "tkipling", "ksimon", "hjones", "amoore");
 $array_userid2 = array("jfulton", "tkipling", "ksimon");

 $array_difference = array_diff($array_userid1, $array_userid2); 
 echo "<br><br>After diff, the new array is: <br>";
 print_r($array_difference);

 $array_userid_dup = array("jfulton", "tkipling", "jfulton", "msimpson", "jfulton");
 $array_unique_ones = array_unique($array_userid_dup);
 echo "<br><br>After unique, the new array is: <br>";
 print_r($array_unique_ones);
 ?>

Here is the output:

 The array is:
 Array ( [0] => 10 [1] => 20 [2] => 30 )
 The sum of array is 60

 After merging, the new array is:
 Array ( [0] => 5 [1] => 10 [2] => 15 [3] => 20 [4] => 25 [5] => 30 )

 After diff, the new array is:
 Array ( [3] => hjones [4] => amoore )

 After unique, the new array is:
 Array ( [0] => jfulton [1] => tkipling [3] => msimpson ) 

The second set presents three additional functions that work on the entire array but accept a user provided callback function. We begin with their signatures:

 $filtered_array  = array_filter($array, 'user_defined_function');
 array_walk($array, 'user_defined_function');
 $total_value = array_reduce($array, 'user_defined_function');

array_filter() filters elements from an array based on a rule (usually provided as a user defined function). array_walk() applies a given rule (or function) to all elements of the array. Due to this, array_walk() can be a very handy function when we need to apply one common rule to all the elements (e.g. checking if the element has a particular value or not). array_reduce() allows us to get a cumulative value (for example, sum) of all the elements present in an array.

We provide an example that shows usage of array_filter(). In this program, for array_filter(), we pass "divisible_by_15" as the callback function. This function returns 1 (true) if the value is divisible by 15. And, only those entries are placed into the new array ($array_filtered) for which the callback returns true.

 <?php
 /* Callback function */
 function divisible_by_15 ($temp_var) {
     if ( ($temp_var % 15) == 0 ) return 1;

     return 0;
 }

 $array_new = array(5, 10, 15, 20, 25, 30);

 $array_filtered = array_filter($array_new, 'divisible_by_15');
 echo "<br><br>After filtering, the new array is: <br>";
 print_r($array_filtered);

 ?>

Here is the output:

 The array is:
 Array ( [0] => 5 [1] => 10 [2] => 15 [3] => 20 [4] => 25 [5] => 30 )

 After filtering, the new array is:
 Array ( [2] => 15 [5] => 30 ) 

Our last example demonstrates the usage of array_walk() and array_reduce(). The examples uses "length_userid" as callback function for array_walk() and "return_length_userid" as callback function for array_reduce().

 <?php
 function length_userid ($var_userid) {
     $var_length = strlen($var_userid);
     echo "The length of userid $var_userid is $var_length <br>";
 }

 function return_length_userid ($sum_so_far, $var_userid) {
     $local_sum = $sum_so_far + strlen($var_userid);
     echo "The sum_so_far is $local_sum <br>";
     return $local_sum; 
 }

 $array_userid = array("jfulton", "tkipling", "ksimon", "msimpson", "hjones", "amoore");

 echo "Doing a walk on array: <br>";
 array_walk($array_userid, 'length_userid');

 echo "<br>Doing a reduce on array: <br>";
 $sum_strlen_userids = array_reduce($array_userid, 'return_length_userid');
 echo "The total string length of all userids is is $sum_strlen_userids <br>";
 ?>

The output (provided below) confirms that array_walk() passes each element of the $array_userid to function "length_userid". Next, array_reduce() uses values returned for each element, and adds them up; finally, it returns the total summation. Please note the usage of "$sum_so_far" variable that gets automatically passed to return_length_userid() function called from array_reduce().

 Doing a walk on array:
 The length of userid jfulton is 7
 The length of userid tkipling is 8
 The length of userid ksimon is 6
 The length of userid msimpson is 8
 The length of userid hjones is 6
 The length of userid amoore is 6

 Doing a reduce on array:
 The sum_so_far is 7
 The sum_so_far is 15
 The sum_so_far is 21
 The sum_so_far is 29
 The sum_so_far is 35
 The sum_so_far is 41
 The total string length of all userids is is 41 




comments powered by Disqus