Program to perform array operations |PHP Program|

Introduction

    Arrays are one of the most powerful and commonly used data structures in PHP, enabling developers to store, manage, and manipulate collections of data. Whether you're working on a small project or a large-scale application, understanding how to perform various array operations is essential. In this blog post, we'll explore a PHP program that demonstrates several key array operations, including displaying an array, sorting, removing duplicates, deleting the last element, reversing the array, and searching within the array.

The PHP Program: Array Operations

Below is a PHP program that provides a web interface for performing various operations on an array. The program is designed to be interactive, allowing users to select an operation from a list and view the results instantly.

        
            <html>
                <body>
                <h2>Array Operations</h2><br>
                <form action="" method="post">
                    <?php
                    // Displaying radio buttons for each array operation
                    echo "<br><input type=radio name=arr value=dis>Display Array";
                    echo "<br><input type=radio name=arr value=srt>Sorted Array";
                    echo "<br><input type=radio name=arr value=usrt>Without duplicate";
                    echo "<br><input type=radio name=arr value=pop>Delete last";
                    echo "<br><input type=radio name=arr value=rev>Array reversal";
                    echo "<br><input type=radio name=arr value=sear>Array search";
                    echo "<br><input type=submit>";
                
                    // Define the array with sample names
                    $name = array("Raju", "Varun", "Vani", "Basheer", "Kumar", "John", "Shani", "Manu", "Kiran", "Sukumar");
                
                    // Handle the form submission
                    if ($_POST) {
                        $val = $_POST['arr'];  // Get the selected operation
                        switch ($val) {
                            case "dis":  // Display Array
                                foreach ($name as $value)
                                    echo "<br>" . $value;
                                break;
                            case "srt":  // Sort Array
                                sort($name);
                                foreach ($name as $value)
                                    echo "<br>" . $value;
                                break;
                            case "usrt":  // Remove Duplicates
                                $uarray = array_unique($name);
                                foreach ($uarray as $value)
                                    echo "<br>" . $value;
                                break;
                            case "pop":  // Delete Last Element
                                array_pop($name);
                                foreach ($name as $value)
                                    echo "<br>" . $value;
                                break;
                            case "rev":  // Reverse Array
                                $revarr = array_reverse($name);
                                foreach ($revarr as $value)
                                    echo "<br>" . $value;
                                break;
                            case "sear":  // Search in Array
                                echo "<br>" . array_search("John", $name, true);
                                break;
                        }
                    }
                    ?>
                </form>
                </body>
                </html>                
        
        


Output





Explanation

1. Display Array:
    This option displays all elements in the array in their original order. It’s a straightforward way to view the contents of the array.
2. Sort Array:
    This operation sorts the array elements in alphabetical order. The sort() function is used, which rearranges the array in ascending order by default.
3. Remove Duplicates:
    If the array contains duplicate elements, this operation removes them. The array_unique() function is utilized to filter out any duplicate values.
4. Delete Last Element:
    This option deletes the last element from the array. The array_pop() function is used to remove the last element, effectively reducing the size of the array by one.
5. Reverse Array:
    This operation reverses the order of the array elements. The array_reverse() function is employed to achieve this, flipping the array so that the last element becomes the first.
6. Search in Array:
    This option searches for a specific element within the array, in this case, "John". The array_search() function returns the index of the element if found, allowing you to locate specific data within the array. 

Conclusion

    Arrays are versatile and powerful data structures that are indispensable in PHP programming. This simple program provides a glimpse into how you can manipulate arrays to perform common operations such as displaying, sorting, removing duplicates, deleting elements, reversing, and searching. Understanding these operations is crucial for any PHP developer, as arrays are a fundamental part of almost every application.

Feel free to expand this program by adding more operations, or by allowing the user to input their own arrays. The possibilities are endless when it comes to array manipulation in PHP! Happy coding!

Post a Comment

0 Comments