Change Array To String Using PHP

In this tutoiral I will teach you on how to Change Array To String using PHP. This code you'll learn how to use array properly. This program will parse an array as a string by looping the array object in foreach function. Feel free to edit and use this to your working programs.

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"/>
  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-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">Change Array To String Using PHP</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <?php
  18.                         $array = [
  19.                                 'Movie' => ['Rampage', 'Black Widow', 'Joker', 'Frozen 2', 'Free Guy'],
  20.                         ];
  21.                        
  22.                         if(!ISSET($_POST['change'])){
  23.                 ?>
  24.                         <div class="col-md-6">
  25.                                 <pre><?php print_r($array)?></pre>
  26.                                 <form method="POST" action="">
  27.                                         <center><button name="change" class="btn btn-primary">Change to String</button></center>
  28.                                 </form>
  29.                         </div>
  30.                 <?php
  31.                         }else{
  32.                        
  33.                                 include'change.php';
  34.                         }
  35.                 ?>
  36.         </div>
  37. </body>
  38. </html>

Creating the Main Function

This code contains the main function of the application. The code will dynamically convert the array object in strong format. To make this just copy and write these block of codes below inside the text editor, then save it as change.php
  1. <div class="col-md-6">
  2.         <table class="table table-bordered">
  3.                 <?php
  4.                         if(ISSET($_POST['change'])){
  5.                                 foreach($array as $section => $list){
  6.                        
  7.                 ?>
  8.                        
  9.                         <tr class="alert-info">
  10.                                 <th><center><?php echo $section?></center></th>
  11.                         </tr>
  12.                 <?php
  13.                                         foreach($list as $key => $value){
  14.                 ?>
  15.                         <tr style="background-color:#fff;">
  16.                                 <td><?php echo $value?></td>
  17.                         </tr>
  18.                 <?php
  19.                                         }
  20.                                
  21.                                 }
  22.                         }
  23.                 ?>
  24.         </table>
  25. </div>
There you have it we successfully created Change 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!

Add new comment