How can you efficiently query specific elements from an array in PHP?

To efficiently query specific elements from an array in PHP, you can use array functions like array_filter() or array_map() along with a custom callback function to filter or map the elements based on certain criteria. This allows you to retrieve only the elements that meet the specified conditions without iterating through the entire array manually.

// Sample array
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Query elements greater than 5
$filteredNumbers = array_filter($numbers, function($num) {
    return $num > 5;
});

print_r($filteredNumbers);