Tony's Array Data Analyzer
Enter several numbers separated by commas (e.g., 85, 92, 78, 100):
CODE FOLLOWS
<?php
include 'includes/header.php';
// Initialize variables
$inputData = "";
$dataArray = [];
$results = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// 1. Gather the string from the form
$inputData = $_POST['dataValues'];
// 2. ARRAY MANIPULATION: Convert string to array using explode()
// We use array_map to ensure every value is treated as a number
$dataArray = array_map('trim', explode(',', $inputData));
$dataArray = array_filter($dataArray, 'is_numeric'); // Remove non-numeric fluff
if (count($dataArray) > 0) {
// 3. INVESTIGATION: Using various array functions
$count = count($dataArray);
$sum = array_sum($dataArray);
$average = $sum / $count;
// Sort the array to find the median or just show organization
sort($dataArray);
$min = $dataArray[0];
$max = end($dataArray);
// Build the output
$results .= "<h4>Analysis Results:</h4>";
$results .= "<ul>";
$results .= "<li>Sorted Values: " . implode(", ", $dataArray) . "</li>";
$results .= "<li>Total Count: **$count**</li>";
$results .= "<li>Highest Value: **$max**</li>";
$results .= "<li>Lowest Value: **$min**</li>";
$results .= "<li>Average: **" . number_format($average, 2) . "**</li>";
$results .= "</ul>";
} else {
$results = "<p style='color:red;'>Please enter valid numeric values separated by commas.</p>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Tony's Array Manipulations</title>
</head>
<body>
<h2>Tony's Array Data Analyzer</h2>
<p>Enter several numbers separated by commas (e.g., 85, 92, 78, 100):</p>
<form method="post" action="">
<input type="text" name="dataValues" style="width: 300px;"
value="<?php echo htmlspecialchars($inputData); ?>" required>
<input type="submit" value="Analyze Array">
</form>
<hr>
<?php echo $results;
include 'includes/footer.php';
?>
</body>
</html>