• Copyright© 2025 xPralak Designs - Joseph Downey
<div id="page">
<?php
include('includes/header.php');
echo '<h1 class="page-heading">Arrays Example 2</h1>';
echo '<h2>Enter Values to Store in an Array</h2>';
echo '<form method="post">
<input type="text" name="values" placeholder="Enter values separated by commas" required>
<button type="submit">Submit</button>
</form>';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$input = $_POST['values'];
$array = array_map('trim', explode(',', $input)); // Convert input into an array
echo "<h3>Original Array:</h3>";
echo "<pre>" . print_r($array, true) . "</pre>";
// Array Length
echo "<h3>Array Length:</h3>";
echo count($array) . " elements<br>";
// Sorting the array
$sortedArray = $array;
sort($sortedArray);
echo "<h3>Sorted Array (Ascending Order):</h3>";
echo "<pre>" . print_r($sortedArray, true) . "</pre>";
// Reverse sorting
$reversedArray = $array;
rsort($reversedArray);
echo "<h3>Reversed Array (Descending Order):</h3>";
echo "<pre>" . print_r($reversedArray, true) . "</pre>";
// Removing duplicate values
$uniqueArray = array_unique($array);
echo "<h3>Array with Unique Values:</h3>";
echo "<pre>" . print_r($uniqueArray, true) . "</pre>";
// Shuffling the array
$shuffledArray = $array;
shuffle($shuffledArray);
echo "<h3>Shuffled Array:</h3>";
echo "<pre>" . print_r($shuffledArray, true) . "</pre>";
// Splitting the array into chunks
$chunkedArray = array_chunk($array, 2);
echo "<h3>Array Split into Chunks (Size 2):</h3>";
echo "<pre>" . print_r($chunkedArray, true) . "</pre>";
// Merging with another array
$extraArray = ["Extra1", "Extra2", "Extra3"];
$mergedArray = array_merge($array, $extraArray);
echo "<h3>Merged with Another Array:</h3>";
echo "<pre>" . print_r($mergedArray, true) . "</pre>";
// Mapping function - Convert to uppercase
$upperArray = array_map('strtoupper', $array);
echo "<h3>Array with Uppercase Values:</h3>";
echo "<pre>" . print_r($upperArray, true) . "</pre>";
// Searching for a specific value
$searchValue = $array[0] ?? ''; // Default to first input value
$searchIndex = array_search($searchValue, $array);
echo "<h3>Searching for '$searchValue':</h3>";
echo $searchIndex !== false ? "Found at index: $searchIndex" : "Not found";
}
include('includes/footer.php');
?>
</div>