Ecco degli esercizi semplici con soluzione per praticare le basi degli array associativi in PHP.
Esercizio 1
Scrivere un programma PHP che dichiara un array associativo di dati personali (nome, cognome, età) e lo stampa.
<?php$person = [ "nome" => "Mario", "cognome" => "Rossi", "età" => 30];
foreach ($person as $key => $value) { echo $key . ": " . $value . "<br>";}?>
Esercizio 2
Scrivere un programma PHP che dichiara un array associativo di voti degli studenti (nome => voto) e calcola la media dei voti.
<?php$grades = [ "Mario" => 80, "Luca" => 90, "Giulia" => 95, "Sara" => 87];
$total = 0;$count = 0;
foreach ($grades as $student => $grade) { $total += $grade; $count++;}
$average = $total / $count;
echo "La media dei voti è: " . $average;?>
Esercizio 3
Scrivere un programma PHP che dichiara un array associativo di prodotti (nome => prezzo) e stampa i prodotti con un prezzo superiore a 50 euro.
<?php$products = [ "Telefono" => 200, "Computer" => 800, "TV" => 600, "Orologio" => 100];
foreach ($products as $product => $price) { if ($price > 50) { echo $product . ": " . $price . " euro<br>"; }}?>
Esercizio 4
Scrivere un programma PHP che dichiara un array associativo di temperature giornaliere (giorno => temperatura) e trova la temperatura massima utilizzando la funzione "max".
<?php$temperatures = [ "Lunedì" => 25, "Martedì" => 28, "Mercoledì" => 30, "Giovedì" => 27];
$maxTemperature = max($temperatures);
echo "La temperatura massima registrata è: " . $maxTemperature . " gradi";?>
Esercizio 5
Scrivere un programma PHP che dichiara un array associativo di parole (parola => lunghezza) e stampa le parole più lunghe.
<?php$words = [ "casa" => 4, "albero" => 6, "macchina" => 7, "giardino" => 8];
$maxLenght = max($words);
foreach ($words as $word => $length) { if ($length == $maxLenght) { echo $word . "<br>"; }}?>
Esercizio 6
Scrivere un programma PHP che dichiara un array associativo di studenti (matricola => nome) e cerca uno studente specifico utilizzando la funzione "array_search".
<?php$students = [ "001" => "Mario", "002" => "Luca", "003" => "Giulia", "004" => "Sara"];
$searchStudent = "Luca";$key = array_search($searchStudent, $students);
if ($key !== false) { echo "Lo studente con nome " . $searchStudent . " ha la matricola: " . $key;} else { echo "Lo studente " . $searchStudent . " non è presente nell'elenco.";}?>
Esercizio 7
Scrivere un programma PHP che dichiara un array associativo di città (nome => popolazione) e verifica se una città specifica è presente utilizzando la funzione "array_key_exists".
<?php$cities = [ "Milano" => 1500000, "Roma" => 2800000, "Napoli" => 1000000, "Torino" => 900000];
$searchCity = "Roma";
if (array_key_exists($searchCity, $cities)) { echo $searchCity . " è presente nell'elenco delle città.";} else { echo $searchCity . " non è presente nell'elenco delle città.";}?>
Esercizio 8
Scrivere un programma PHP che dichiara un array associativo di prodotti (codice => quantità) e aggiorna la quantità di un prodotto specifico utilizzando l'operatore di assegnazione.
<?php$products = [ "P01" => 10, "P02" => 5, "P03" => 20];
$productCode = "P02";$newQuantity = 8;
$products[$productCode] = $newQuantity;
foreach ($products as $code => $quantity) { echo "Codice prodotto: " . $code . ", Quantità: " . $quantity . "<br>";}?>
Esercizio 9
Scrivere un programma PHP che dichiara un array associativo di mesi dell'anno (numero => nome) e stampa i mesi in ordine crescente utilizzando la funzione "asort".
<?php$months = [ 1 => "Gennaio", 4 => "Aprile", 2 => "Febbraio", 3 => "Marzo"];
asort($months);
foreach ($months as $number => $name) { echo "Numero: " . $number . ", Mese: " . $name . "<br>";}?>
Esercizio 10
Scrivere un programma PHP che dichiara un array associativo di studenti (matricola => nome) e stampa i nomi degli studenti in ordine alfabetico utilizzando la funzione "ksort".
<?php$students = [ "002" => "Luca", "001" => "Mario", "004" => "Sara", "003" => "Giulia"];
ksort($students);
foreach ($students as $key => $name) { echo "Matricola: " . $key . ", Nome: " . $name . "<br>";}?>