Sitemap / Advertise

Information



Tags



Share

How to use array filter, array map, array merge, and array combine to arrange arrays in PHP

Advertisement:


read_later

Read Later

Keywords



Keywords



read_later

Read Later

Information

Tags

Share





Advertisement

Advertisement




Definition

If you work with an enormous amount of data stored as arrays or multi-dimensional arrays in PHP, for instance, in WordPress, you will need to arrange and sort these arrays to chart the current and further changes in the server database. Like JavaScript, you can use array_filter, array_map functions in addition to array_merge and array_combine functions specific to PHP. In this tutorial, I shall show you how to sort the customer service revenue in regards to user names.

array_filter()(1)

This function passes each value of the input array to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.

array_map()(2)

The array_map() function sends each value of an array to a user-made function, and returns an array with new values, given by the user-made function.

array_merge()(3)

The array_merge() function merges one or more arrays into one array. If two or more array elements have the same key, the last one overrides the others.

array_combine()(4)

The array_combine() function creates an array by using the elements from one "keys" array and one "values" array. Both arrays must have equal number of elements.

Code

Define usernames, customer fees, and customer payments.

For each customer fee bigger than 15, return true in the callback function.

For each customer payment smaller than 1000, return true in the callback function.

If the customer fee is either equal to or bigger than the threshold(20), save its second power(the exponent is 2).

To get the total revenue, merge fees and payments.

Assign each variable in the total array to a specific user name in the usernames array.

<?php
$usernames = array("John", "Clark", "Bruce", "Dinah", "Peter", "Chris");

$customerfees = array(15, 30, 5, 1, 3, 8, 16, 21);
$customerpayment = array(250, 1050, 1500, 120, 100, 1112, 1100);

$filterFee = array_filter($customerfees, function($val){
if($val > 15){ return true; }else{ return false; }
});

$filterPayment = array_filter($customerpayment, function($val){
if($val < 1000){ return true; }else{ return false; }
});

$mapFee = array_map(function($val){
if($val >= 20){
return $val * $val;
}else{
return $val;
}
} ,$filterFee);

$total = array_merge($filterPayment, $mapFee);

$assignedUsername = array_combine($usernames, $total);

echo "<p>Clark = ".$assignedUsername['Clark']."</p>";
echo "<p>Bruce = ".$assignedUsername['Bruce']."</p>";
echo "<p>Chris = ".$assignedUsername['Chris']."</p>";

?>

Result:

Clark = 120

Bruce = 100

Chris = 441

References

(1) https://www.w3schools.com/php/func_array_filter.asp

(2) https://www.w3schools.com/php/func_array_map.asp

(3) https://www.w3schools.com/php/func_array_merge.asp

(4) https://www.w3schools.com/php/func_array_combine.asp