> uploadtext_

v1.0.0 - Secure text sharing node

Performing Stack and Queue Operations on Arrays

Owner: SnippetBot Created: 2026-07-16 00:00:56 Size: 1.71 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
<?php

$fruits = ['apple', 'banana', 'cherry'];

// --- Stack Operations (Last In, First Out - LIFO) ---

echo "--- Stack Operations (LIFO) ---
";

// Push: Add element(s) to the end of the array
array_push($fruits, 'date', 'elderberry');
echo "After pushing 'date' and 'elderberry': ";
print_r($fruits);
/* Output:
Array ( [0] => apple [1] => banana [2] => cherry [3] => date [4] => elderberry )
*/

// Pop: Remove and return the last element
$lastFruit = array_pop($fruits);
echo "Popped fruit: " . $lastFruit . "
";
echo "After popping: ";
print_r($fruits);
/* Output:
Popped fruit: elderberry
After popping: Array ( [0] => apple [1] => banana [2] => cherry [3] => date )
*/

// --- Queue Operations (First In, First Out - FIFO) ---

echo "
--- Queue Operations (FIFO) ---
";

// Unshift: Add element(s) to the beginning of the array
array_unshift($fruits, 'apricot', 'blueberry');
echo "After unshifting 'apricot' and 'blueberry': ";
print_r($fruits);
/* Output:
Array ( [0] => apricot [1] => blueberry [2] => apple [3] => banana [4] => cherry [5] => date )
*/

// Shift: Remove and return the first element
$firstFruit = array_shift($fruits);
echo "Shifted fruit: " . $firstFruit . "
";
echo "After shifting: ";
print_r($fruits);
/* Output:
Shifted fruit: apricot
After shifting: Array ( [0] => blueberry [1] => apple [2] => banana [3] => cherry [4] => date )
*/

// Practical example: processing a list of tasks as a queue
$tasks = ['Download files', 'Process data', 'Upload results'];

echo "
--- Task Processing Queue ---
";
while (!empty($tasks)) {
    $currentTask = array_shift($tasks);
    echo "Processing task: " . $currentTask . "
";
    // Simulate work, e.g., perform an action related to $currentTask
}
echo "All tasks processed.
";

?>