How can an array value be filtered based on specific criteria, such as "CLOSE" status for a sensor ID?

To filter an array based on specific criteria, such as "CLOSE" status for a sensor ID, you can use the array_filter() function in PHP. This function allows you to specify a callback function that determines whether each element in the array should be included in the filtered result. You can use this callback function to check if the sensor ID has a "CLOSE" status and only include those elements in the filtered array.

// Sample array with sensor data
$sensorData = [
    ['sensor_id' => 1, 'status' => 'OPEN'],
    ['sensor_id' => 2, 'status' => 'CLOSE'],
    ['sensor_id' => 3, 'status' => 'CLOSE'],
    ['sensor_id' => 4, 'status' => 'OPEN'],
];

// Filter the array to only include elements with 'CLOSE' status for sensor ID
$filteredData = array_filter($sensorData, function($sensor) {
    return $sensor['status'] == 'CLOSE';
});

// Output the filtered array
print_r($filteredData);