Skip to main content

PHP - Simple Array Merge


In this tutorial we will create a Simple Array Merge using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-2"></div>
  14. <div class="col-md-8 well">
  15. <h3 class="text-primary">PHP - Simple Array Merge</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <form method="POST" action="">
  18. <center><button class="btn btn-primary" name="merge"><span class="glyphicon glyphicon-check"></span> Merge</button></center>
  19. </form>
  20. <?php include 'merge.php'?>
  21. </div>
  22. </body>
  23. </html>

Creating the Main Function

This code contains the main function of the application. This code will merge the arrays into one whole array. To make this just simple copy and write the this block of codes inside the text editor, then save it as merge.php.

  1. <?php
  2. $student1 = array(
  3. array("firstname" => "John", "lastname" => "Smith", "year&sec" => "1B")
  4. );
  5. $student2 = array(
  6. array("firstname" => "Claire", "lastname" => "Temple", "year&sec" => "2A")
  7. );
  8. ?>
  9. <div class="col-md-6">
  10. <h3>Student1</h3>
  11. <?php
  12.  
  13. echo "<pre>";
  14. print_r($student1);
  15. echo "</pre>";
  16. ?>
  17.  
  18. <h3>Student2</h3>
  19. <?php
  20. echo "<pre>";
  21. print_r($student2);
  22. echo "</pre>";
  23. ?>
  24. </div>
  25. <br /><br /><br /><br /><br /><br />
  26. <div class="col-md-6">
  27. <?php
  28. if(ISSET($_POST['merge'])){
  29. $students = array_unique(array_merge($student1, $student2), SORT_REGULAR);
  30. echo "<pre>";
  31. print_r($students);
  32. echo "</pre>";
  33. }
  34.  
  35. ?>
  36. </div>

There you have it we successfully created Simple Array Merge using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.