PHP - Simple Array Merge

Submitted by: 
Visitors have accessed this post 204 times.

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

And this is the link for the bootstrap that i used for the layout design

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="<a href="">Sourcecodester</a>
  11. " rel="nofollow">">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-2"></div>
  15. <div class="col-md-8 well">
  16. <h3 class="text-primary">PHP - Simple Array Merge</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <form method="POST" action="">
  19. <center><button class="btn btn-primary" name="merge"><span class="glyphicon glyphicon-check"></span> Merge</button></center>
  20. </form>
  21. <?php include 'merge.php'?>
  22. </div>
  23. </body>
  24. </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
  13. echo "<pre>";
  14. print_r($student1);
  15. echo "</pre>";
  16. ?>
  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. }
  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!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.