Arrays — Nataliia’s Demo
Enter a few things, comma-separated. Then I’ll play with the arrays 🙂
Enter a few things, comma-separated. Then I’ll play with the arrays 🙂
<?php
// arrays_demo.php
include('includes/header.php');
error_reporting(E_ALL);
// --- tiny CSS so the blocks look tidy ---
echo '<style>
.wrap{max-width:760px;margin:0 auto;padding:0 12px;font-family:Arial,sans-serif}
pre{background:#e8eeff;border:1px solid #c5cbe1;border-radius:4px;padding:8px 10px;
font:12px/1.25 Consolas,"Courier New",monospace;white-space:pre-wrap;word-break:break-word;
max-height:320px;overflow:auto;margin:8px 0}
h1,h2{margin:.4rem 0}
.hr{border-top:1px solid #bbb;margin:14px 0}
label{display:block;margin:.4rem 0 .1rem}
input[type=text]{width:100%;max-width:680px;padding:6px}
.row{margin:.5rem 0}
button{padding:.45rem .8rem;border:1px solid #567;cursor:pointer;border-radius:6px}
</style>';
function box($title, $data){
echo "<h3>$title</h3><pre>";
if (is_array($data)) { print_r($data); }
else { echo $data; }
echo "</pre>";
}
// Step & inputs
$step = isset($_POST['step']) ? 1 : 0;
?>
<div class="wrap">
<h1>Arrays — Nataliia’s Demo</h1>
<?php if(!$step): ?>
<p>Enter a few things, comma-separated. Then I’ll play with the arrays 🙂</p>
<form method="post">
<div class="row">
<label for="fr">Favorite fruits (comma separated):</label>
<input type="text" id="fr" name="fruits" placeholder="apple, banana, mango, pear">
</div>
<div class="row">
<label for="nums">Numbers (comma separated):</label>
<input type="text" id="nums" name="numbers" placeholder="10, 7, 42, 3, 7">
</div>
<div class="row">
<label for="fav">One favorite thing (for search demo):</label>
<input type="text" id="fav" name="fav" placeholder="banana">
</div>
<input type="hidden" name="step" value="1">
<button type="submit">Show Array Magic</button>
</form>
<?php else:
// ------ Build arrays from form ------
$fruits_raw = isset($_POST['fruits']) ? $_POST['fruits'] : '';
$numbers_raw = isset($_POST['numbers']) ? $_POST['numbers'] : '';
$fav = isset($_POST['fav']) ? trim($_POST['fav']) : '';
// helper: split, trim, drop empties
function clean_list($s){
$parts = explode(',', $s);
foreach ($parts as &$p) { $p = trim($p); }
$parts = array_values(array_filter($parts, function($x){ return $x !== ''; }));
return $parts;
}
$fruits = clean_list($fruits_raw);
$numbers = clean_list($numbers_raw);
// make numbers truly numeric
$numbers = array_map(function($n){ return (is_numeric($n) ? 0 + $n : null); }, $numbers);
$numbers = array_values(array_filter($numbers, function($n){ return $n !== null; }));
echo '<div class="hr"></div>';
box('Original Fruits', $fruits);
box('Original Numbers', $numbers);
// ------ Easy counts & uniques ------
box('Fruit Count / Unique', [
'count' => count($fruits),
'unique' => array_values(array_unique($fruits)),
'counts' => array_count_values($fruits)
]);
// ------ Sorting ------
$f_sorted = $fruits; sort($f_sorted, SORT_NATURAL | SORT_FLAG_CASE);
$n_sorted = $numbers; sort($n_sorted, SORT_NUMERIC);
box('Fruits sorted A→Z', $f_sorted);
box('Numbers sorted ↑', $n_sorted);
// reverse & rsort
$f_rev = array_reverse($f_sorted);
$n_desc = $numbers; rsort($n_desc, SORT_NUMERIC);
box('Fruits reversed (Z→A quick)', $f_rev);
box('Numbers sorted ↓', $n_desc);
// ------ Transformations ------
$f_upper = array_map('strtoupper', $fruits);
$f_len_ge5 = array_values(array_filter($fruits, function($x){ return mb_strlen($x) >= 5; }));
box('Fruits to UPPER (array_map)', $f_upper);
box('Fruits length ≥ 5 (array_filter)', $f_len_ge5);
box('Fruits as CSV (implode)', implode(', ', $fruits));
// ------ Stack / Queue behaviors ------
$stack = $fruits;
array_push($stack, 'kiwi'); // push
$popped = array_pop($stack); // pop (removes kiwi)
array_unshift($stack, 'first'); // unshift
$shifted = array_shift($stack); // shift (removes 'first')
box('Stack after push/pop & unshift/shift', [
'final' => $stack,
'popped' => $popped,
'shifted' => $shifted
]);
// ------ Search ------
$target = $fav ?: (isset($fruits[0]) ? $fruits[0] : '');
$exists = in_array($target, $fruits, true) ? 'YES' : 'NO';
$idx = array_search($target, $fruits, true);
box("Search for '$target'", [
'in_array' => $exists,
'array_search'=> $idx
]);
// ------ Merge & Combine ------
$merged = array_merge($fruits, ['dragonfruit','plum']);
$lengths = array_map('mb_strlen', $fruits);
$fruit_len_map = (count($fruits) === count($lengths)) ? array_combine($fruits, $lengths) : [];
asort($fruit_len_map, SORT_NUMERIC);
box('Merged Fruits (+ 2 more)', $merged);
box('Associative map fruit => length (asort by value)', $fruit_len_map);
// ------ Numbers: basic stats ------
$sum = array_sum($numbers);
$avg = $numbers ? round($sum / count($numbers), 2) : 0;
box('Number Stats', [
'sum' => $sum,
'min' => $numbers ? min($numbers) : null,
'max' => $numbers ? max($numbers) : null,
'avg' => $avg
]);
// ------ Fun: shuffle without destroying original ------
$shuf = $fruits; shuffle($shuf);
box('Fruits shuffled', $shuf);
echo '<p><a href="arrays_demo.php">↩︎ Start over</a></p>';
endif; ?>
</div>
<?php include('includes/footer.php'); ?>