PHP - Combining Two Arrays

Submitted by: 
Language: 
Visitors have accessed this post 300 times.

In this tutorial we will create a Combining Two Arrays 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...

Before we get 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.

Lastly, 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 you 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 - Combining Two Arrays</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <?php include 'array.php'?>
  18. <br style="clear:both;"/>
  19. <form method="POST" action="">
  20. <center><button class="btn btn-primary" name="combine"><span class="glyphicon glyphicon-check"></span> Combine</button></center>
  21. </form>
  22. </div>
  23. </body>
  24. </html>

Creating the Main Function

This code contains the main function of the application. This code will combine the two arrays into one when the button is clicked. To do this just copy and write the code inside the text editor, then save it as array.php.

  1. <div class="col-md-6">
  2. <h3>Book1</h3>
  3. <?php
  4. $book1 = array(
  5. array("title" => "The Feral Detective.", "author" => "Jonathan Lethem"),
  6. array("title" => "Everything Under", "author" => "Daisy Johnson"),
  7. );
  8. $book2 = array(
  9. array("title" => "Warlight", "author" => "Michael Ondaatje"),
  10. array("title" => "In Our Mad and Furious City", "author" => "Guy Gunaratne"),
  11. );
  12.  
  13. echo "<pre>";
  14. print_r($book1);
  15. echo "</pre>";
  16.  
  17. ?>
  18. </div>
  19. <div class="col-md-6">
  20. <h3>Book2</h3>
  21. <?php
  22. echo "<pre>";
  23. print_r($book2);
  24. echo "</pre>";
  25. ?>
  26. </div>
  27. <div class="col-md-3"></div>
  28. <div class="col-md-6">
  29. <?php
  30. if(ISSET($_POST['combine'])){
  31. $books = array_unique(array_merge($book1, $book2), SORT_REGULAR);
  32. echo "<pre>";
  33. print_r($books);
  34. echo "</pre>";
  35. }
  36.  
  37. ?>
  38. </div>

There you have it we successfully created Combining Two Arrays 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.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.