PHP Array : Comprehensive Array Demo
Input Your Travel Details
Please complete the form fields above and click Process Arrays to build your structures.
Please complete the form fields above and click Process Arrays to build your structures.
<?php
include('includes/header.php');
error_reporting(E_ALL); // set error reporting to all
echo('<h1>PHP Array : Comprehensive Array Demo</h1>');
// ==========================================
// HTML INPUT FORM
// ==========================================
echo('<h2>Input Your Travel Details</h2>');
?>
<form method="POST" action="" style="background: #f9f9f9; padding: 15px; border: 1px solid #ccc; max-width: 500px; margin-bottom: 20px;">
<h3>Destination</h3>
<label>City 1: </label><input type="text" name="city1" value="<?php echo isset($_POST['city1']) ? htmlspecialchars($_POST['city1']) : 'Tokyo'; ?>"><br>
<label>Country 1: </label><input type="text" name="country1" value="<?php echo isset($_POST['country1']) ? htmlspecialchars($_POST['country1']) : 'Japan'; ?>"><br>
<label>Days 1: </label><input type="text" name="days1" value="<?php echo isset($_POST['days1']) ? htmlspecialchars($_POST['days1']) : '7'; ?>"><br><br>
<h3>Destination 2</h3>
<label>City 2: </label><input type="text" name="city2" value="<?php echo isset($_POST['city2']) ? htmlspecialchars($_POST['city2']) : 'Paris'; ?>"><br>
<label>Country 2: </label><input type="text" name="country2" value="<?php echo isset($_POST['country2']) ? htmlspecialchars($_POST['country2']) : 'France'; ?>"><br>
<label>Days 2: </label><input type="text" name="days2" value="<?php echo isset($_POST['days2']) ? htmlspecialchars($_POST['days2']) : '5'; ?>"><br><br>
<input type="submit" name="submit" value="Process Arrays">
</form>
<?php
// Process everything only if form is submitted
if (isset($_POST['submit'])) {
// ==========================================
// 1. POPULATING ASSOCIATIVE ARRAYS
// ==========================================
echo('<h2>1. Associative Arrays </h2>');
echo('<p>Populating associative arrays using text keys to group destination details with mixed types </p>');
$dest1_assoc = array(
'city' => $_POST['city1'],
'country' => $_POST['country1'],
'days' => (int)$_POST['days1'] // Storing as integer
);
$dest2_assoc = array(
'city' => $_POST['city2'],
'country' => $_POST['country2'],
'days' => (int)$_POST['days2'] // Storing as integer
);
echo('<p>Destination 1 Associative Array:</p>');
echo('<pre>');
print_r($dest1_assoc);
echo('</pre>');
echo('<p>Destination 2 Associative Array:</p>');
echo('<pre>');
print_r($dest2_assoc);
echo('</pre>');
// ===============================================
// 2. POPULATING MULTIDIMENSIONAL & INDEXED ARRAYS
// ===============================================
echo('<h2>2. Multidimensional & Indexed Arrays (added or removed elements)</h2>');
echo('<p>Creating an indexed multidimensional array that holds our associative data blocks.</p>');
// Initializing an empty master travel itinerary array (Indexed)
$master_itinerary = array();
// Pushing our associative structures inside it
$master_itinerary[] = $dest1_assoc;
$master_itinerary[] = $dest2_assoc;
echo('<p>Master Itinerary Multidimensional Array:</p>');
echo('<pre>');
print_r($master_itinerary);
echo('</pre>');
// ==========================================
// 3. ARRAY FUNCTIONS DEMO
// ==========================================
echo('<h2>3. Array Functions Demo</h2>');
// Function 1: count()
echo('<p><strong>1. count()</strong> - Counts the elements inside the Multidimensional Array:</p>');
$total_destinations = count($master_itinerary);
echo('<p>Total destinations planned: ' . $total_destinations . '</p>');
// Function 2: array_push()
echo('<p><strong>2. array_push()</strong> - Injecting a 3rd temporary option into our structure:</p>');
$dest3_assoc = array(
'city' => 'Rome',
'country' => 'Italy',
'days' => 4
);
array_push($master_itinerary, $dest3_assoc);
echo('<pre>');
print_r($master_itinerary);
echo('</pre>');
// Function 3: array_pop()
echo('<p><strong>3. array_pop()</strong> - Removing the last added item:</p>');
$popped_destination = array_pop($master_itinerary);
echo('<p>Removed Destination Data Chunk:</p>');
echo('<pre>');
print_r($popped_destination);
echo('</pre>');
// Function 4: array_merge()
echo('<p><strong>4. array_merge()</strong> - Merging our master list with a standalone backup indexed array:</p>');
$backup_cities = array('London', 'Cairo', 'New York');
// Flattening just the cities into an indexed vector to demonstrate clean string merging
$only_cities = array($master_itinerary[0]['city'], $master_itinerary[1]['city']);
$merged_city_list = array_merge($only_cities, $backup_cities);
echo('<pre>');
print_r($merged_city_list);
echo('</pre>');
// Function 5: sort()
echo('<p><strong>5. sort()</strong> - Sorting the merged list array in alphabetical order:</p>');
sort($merged_city_list);
echo('<pre>');
print_r($merged_city_list);
echo('</pre>');
} else {
echo('<p style="color: blue;">Please complete the form fields above and click <strong>Process Arrays</strong> to build your structures.</p>');
}
include('includes/footer.php');
?>