All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Print Multidimensional Array In PHP Using For Loop

Last Updated : Mar 11, 2024

How To Print Multidimensional Array In PHP Using For Loop

In this tutorial we will show you the solution of how to print multidimensional array in PHP using for loop, here we defining multidimensional array which is done by inserting each values with our own choice of index.

Then using for loop we printed each values of array in correct index order when we define multidimensional array we inserted values are unordered wise so when seeing result we can found correct ordered values with respective index.

Step By Step Guide On How To Print Multidimensional Array In PHP Using For Loop :-

Here we defined values for multidimensional array then inserted values by manually at index as our wish like ‘in first index $a [0][0] we inserted value A4’ so we inserted values randomly.

Using for loop we printed multidimensional array $a.

When we printing multidimensional array we need to use two for loops because in first for loop we iterate values up to row count within that we defined another for loop for iterate column values likewise we iterated row and columns then printed all values in order wise.

<?php
$a [1][0]="A1";
$a [1][1]="A2";
$a [0][2]="A3";
$a [0][0]="A4";
$a [0][1]="A5";
$a [1][2]="A6";
for($b=0;$b<count($a);$b++){
    for($c=0;$c<count($a [$b]);$c++){
        echo $a [$b][$c], "<br>";
    }
    echo "<br>";
}
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. We defining values ‘A1,A2,A3,A4,A5,A6’ for multidimensional array and manually we inserting those values into array $a index with randomly.
  4. Like we inserted A4 value to first index of array $a[0][0], here we just stored all values at our own choices of index location so it will look like unordered.
  5. Then we defined for loop within that we used variable $b for iterate array value then this loop will process up to 2 because it refers array row count.
  6. Within that we defined another loop it process until it reach $a[$b] count. Variable $a[$b] count is 3 it refers column count so here we printed index of ‘$a[0][0],$a[0][1],$a[0][2]’ values ‘A4,A5,A3’ then for condition failed.
  7. So it returns to first for loop then variable $b value increased by 1 then iteration will starts it point next row of ‘1’.
  8. Next for loop repeated same process as we seen above then printed index of ‘$a[1][0],$a[1][1],$a[1][2]’ respective values ‘A1,A2,A6’ on webpage screen.

Conclusion :-

In conclusion we are able to know how to print multidimensional array using for loop in php.

First we need to start our xampp server then we load this program on browser we can see result of array values displayed on webpage.

First three values ‘A4,A5,A3’ refers first row in array $a then next three values ‘A1,A2,A6’ refers second row in array $a.

We can also define array with different value as our wish.

I hope this tutorial on how to print multidimensional array in PHP using for loop helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪