How can arrays be utilized in PHP to assign different colors to table rows based on specific criteria?

To assign different colors to table rows based on specific criteria in PHP, you can use an array to store the different colors and then iterate through the table rows, applying the colors based on the criteria. You can use a conditional statement to determine which color to assign to each row.

<?php
// Array of colors
$colors = array("red", "blue", "green");

// Sample table rows with specific criteria
$tableRows = array(
    array("name" => "John", "age" => 25),
    array("name" => "Jane", "age" => 30),
    array("name" => "Alice", "age" => 20)
);

// Iterate through table rows and assign colors based on age
foreach ($tableRows as $row) {
    if ($row["age"] < 25) {
        $color = $colors[0]; // red
    } elseif ($row["age"] >= 25 && $row["age"] < 30) {
        $color = $colors[1]; // blue
    } else {
        $color = $colors[2]; // green
    }

    // Output table row with assigned color
    echo "<tr style='background-color: $color;'><td>{$row['name']}</td><td>{$row['age']}</td></tr>";
}
?>