PHP Practical Slip 6 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); // merges arrays
$union = array_unique($union); // remove duplicates

echo "<h3>Union of Array1 and Array2:</h3>";
print_r($union); // display array
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