How To Get Single Input Array Data Through For Loop In PHP

admin_img Posted By Bajarangi soft , Posted On 08-01-2021

In PHP we can also get single input array with for loop.So today we discuss how to do it.

How To Get Single Input Array Data Through For Loop In PHP

Let start,
Step 1:Create index.php file in xampp\htdocs\array\ folder and implement below code in it.

<!DOCTYPE html>
<html>
<head>
    <title>How To Store Single Input Array Data Through Loop In PHP</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<style>
    .bg_form{
        background: lightblue;
        border-radius: 20px;
        padding: 10px;
    }
</style>
<body>
<div class="container">
    <div class="text-center">
        <h1>How To Store Single Input Array Data Through Loop In PHP</h1>
    </div>
    <h2></h2>
        <form action="../demo/arrayfetch.php" method="post">
           <div class="col-md-12 bg_form">
               <div class="col-md-6">
                   <h4>Animals</h4>
                   <input class="form-control" name="Animals[]"/><br>
                   <input class="form-control" name="Animals[]"/><br>
                   <input class="form-control" name="Animals[]"/><br>
                   <input class="form-control" name="Animals[]"/><br>
                   <input class="form-control" name="Animals[]"/><br>
                   <input class="form-control" name="Animals[]"/><br>
                   <input type="submit" value="save" class="btn btn-success">
               </div>
           </div>
        </form>
    </div>
    <br>
</body>
</html>



Step 2:Now create fetcharray.php file in xampp\htdocs\array\ folder and implement below code in it.
<?php

if(isset($_POST['Animals'])){
        for($i = 0; $i<count($_POST['Animals']); $i++){
            $pr_var = [];
            $pr_var['Animals'] = $_POST['Animals'][$i];
            print_r($pr_var);

            // save your data in database
        }

}
?>

Step 3:Run your file in browser as below url
http://localhost/array/index.php

Related Post