How can PHP beginners efficiently filter and calculate values from multiple arrays based on a specific criterion like last name?

To efficiently filter and calculate values from multiple arrays based on a specific criterion like last name, beginners can use the array_filter function along with a custom callback function to filter out the arrays that meet the specified criterion. They can then use array_map or a loop to calculate the desired values from the filtered arrays.

// Sample arrays with last names
$users = [
    ['first_name' => 'John', 'last_name' => 'Doe', 'age' => 30],
    ['first_name' => 'Jane', 'last_name' => 'Smith', 'age' => 25],
    ['first_name' => 'Alice', 'last_name' => 'Doe', 'age' => 35],
];

// Filter arrays based on last name
$filteredUsers = array_filter($users, function($user) {
    return $user['last_name'] == 'Doe';
});

// Calculate values from filtered arrays
$totalAge = array_sum(array_column($filteredUsers, 'age'));
echo "Total age of users with last name 'Doe' is: $totalAge";