Ejercicios
1. Del siguiente arreglo:
$numbers = [1, 5, 23, 4, 12, 45, 78, 6, 8.2, 26];
Filtrar los números que son mayores a 20.
Solución:
$results = array_filter($numbers, function ($number) {
return $number > 20;
});
foreach ($results as $result) {
echo $result;
}
2. Del siguiente arreglo:
$students = [
[
'name' => 'Carlos',
'score' => 12
],
[
'name' => 'Daniel',
'score' => 10
],
[
'name' => 'Sofia',
'score' => 18,
],
[
'name' => 'Maria',
'score' => 11,
],
[
'name' => 'Alex',
'score' => 9,
]
];
Obtener la lista de estudiantes aprobados y desaprobados.
Solución:
$approved = array_filter($students, function ($student) {
return $student['score'] >= 11;
});
foreach ($approved as $student) {
echo $student['name'] . ' ' . $student['score'] . '<br>';
}
$disapproved = array_filter($students, function ($student) {
return $student['score'] <= 10;
});
foreach ($disapproved as $student) {
echo $student['name'] . ' ' . $student['score'] . '<br>';
}
No hay comentarios:
Publicar un comentario