How can I efficiently separate and extract specific data from an array in PHP?
To efficiently separate and extract specific data from an array in PHP, you can use array functions like array_filter, array_map, or a foreach loop to iterate through the array and extract the desired data based on specific conditions or criteria.
// Sample array with data
$data = [
['name' => 'Alice', 'age' => 25],
['name' => 'Bob', 'age' => 30],
['name' => 'Charlie', 'age' => 35]
];
// Extract names of individuals who are older than 28
$filteredData = array_filter($data, function($item) {
return $item['age'] > 28;
});
$names = array_map(function($item) {
return $item['name'];
}, $filteredData);
print_r($names);