Return Array As A String in PHP

June 23, 2020
PHP
return array as a string in php

In this tutorial, we will create a Return Array As A String using PHP. This code can convert an array of data as a readable string format in the table when the user clicks the button. The system uses the PHP POST method to launch a specific way that will parse the array as a string format by using for loop to break down the variety and display as text. This is a user-friendly kind of program feel free to modify it.

We will be using PHP as a scripting language and interpreter that is used primarily on any web server, including xamp, wamp, etc. It is being used to any popular websites, and it has a modern technology that can easily be used by the next generation.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the 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 a copy of the form and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" name="viewport" content="width=device-width"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
    </head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Return Array As A String</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <?php
            $hero = [
                'Marvel' => ['Captain America', 'Iron Man', 'Incredible Hulk', 'Thor', 'Black Widow'],
                'DC' => ['Superman', 'Batman', 'Wonderwoman', 'Flash', 'Aquaman']
            ];
 
            if(!ISSET($_POST['convert'])){
        ?>
            <div class="col-md-2"></div>
            <div class="col-md-8">
                <pre style='height:400px;'><?php print_r($hero)?></pre>
            </div>
            <form method="POST" action="">
                <center><button name="convert" class="btn btn-primary">Array to String</button></center>
            </form>
        <?php
            }else{
 
                include'string.php';
            }
        ?>
    </div>
</body>
</html>

Creating the Main Function

This code contains the main function of the application. This code will display an array when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as string.php

<div class="col-md-6">
    <table class="table table-bordered">
        <?php
            if(ISSET($_POST['convert'])){
                foreach($hero as $section => $list){
 
                if($section=="Marvel"){
        ?>
 
            <tr class="alert-info">
                <th><center><?php echo $section?></center></th>
            </tr>
        <?php
                        foreach($list as $key => $value){
        ?>
            <tr style="background-color:#fff;">
                <td><?php echo $value?></td>
            </tr>
        <?php
                        }
                    }
                }
            }
        ?>
    </table>
</div>
<div class="col-md-6">
    <table class="table table-bordered">
        <?php
            if(ISSET($_POST['convert'])){
                foreach($hero as $section => $list){
 
                if($section=="DC"){
        ?>
 
            <tr class="alert-info">
                <th><center><?php echo $section?></center></th>
            </tr>
        <?php
                        foreach($list as $key => $value){
        ?>
            <tr style="background-color:#fff;">
                <td><?php echo $value?></td>
            </tr>
        <?php
                        }
                    }
                }
            }
        ?>
    </table>
</div>
<br />
<center><a class="btn btn-success" href="index.php"><span class="glyphicon glyphicon-refresh"></span> Reset</a></center>

There you have it we successfully created Return Array As A 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!

https://www.campcodes.com/

This is a free education portal. You can use every source code in your project without asking permission to the author. Share our website to everyone to make our community of programmers grow more.

    , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

    Leave a Reply

    Your email address will not be published. Required fields are marked *