PHP - Convert Array To String

In this tutorial we will create a Convert Array To String using PHP. PHP is a server-side scripting language designed primarily for web development. It is a lean and consistent way to access databases. This means developers can write portable code much easier. 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"/>
  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 - Convert Array To String</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <?php
  18.                         $cars = [
  19.                                 'Jaguar' => ['Jaguar F – Pace', 'Jaguar F – Type (SVR)', 'Jaguar F-Type Convertible'],
  20.                                 'Ferrari' => ['Ferrari 488', 'LaFerrari', 'Ferrari FF'],
  21.                                 'Mini' => ['MINI Coupe', 'MINI Cooper', 'MINI Roadster'],
  22.                                 'Porsche' => ['Porsche Cayman', 'Porsche Boxter GTS', 'Porsche Boxter']
  23.                         ];
  24.                 ?>
  25.                         <div class="col-md-7">
  26.                                 <pre style='height:400px;'><?php print_r($cars)?></pre>
  27.                                 <form method="POST" action="">
  28.                                         <center><button name="convert" class="btn btn-primary"><span class="glyphicon glyphicon-upload"></span> Convert to String</button></center>
  29.                                 </form>
  30.                         </div>
  31.                         <div class="col-md-5">
  32.                                 <?php include 'convert.php'?>
  33.                         </div>
  34.         </div>
  35. </body>
  36. </html>

Creating the Main Function

This code contains the main function of the application. This code will get the array of data, then will display into a HTML table. To do this just kindly copy and write these block of codes inside the text editor, then save it as convert.php
  1. <table class="table table-bordered">
  2.         <?php
  3.                 if(ISSET($_POST['convert'])){
  4.                         foreach($cars as $section => $list){
  5.         ?>
  6.                 <tr class="alert-success">
  7.                         <th><center><?php echo $section?></center></th>
  8.                 </tr>
  9.         <?php
  10.                                 foreach($list as $key => $value){
  11.         ?>
  12.         <tr style="background-color:#fff;">
  13.                 <td><?php echo $value?></td>
  14.         </tr>
  15.         <?php
  16.                                 }
  17.                         }
  18.                 }
  19.         ?>
  20. </table>
There you have it we successfully created Convert Array To String 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!!!
Tags

Add new comment