PHP Practical Slip 15 Q.A

Write PHP program to perform the following operations on Indexed Array:
Union of two arrays
Traverse the array elements in random order

<?php
// Define two indexed arrays
$array1 = [1, 2, 3, 4, 5];
$array2 = [4, 5, 6, 7, 8];

// Union of two arrays
$union = array_merge($array1, $array2); // Merge arrays
$union = array_unique($union);          // Remove duplicates

echo "<h3>Union of Array1 and Array2:</h3>";
print_r($union);
echo "<br><br>";

// Traverse array elements in random order
$randomArray = $union; // Copy the union array
shuffle($randomArray); // Shuffle elements randomly

echo "<h3>Array Elements in Random Order:</h3>";
foreach ($randomArray as $value) {
    echo $value . " ";
}
?>
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top