In this tutorial, I will explain the best top 4 methods to print php array with some examples so that you can easily print any types of array on the web page.
Even you will learn to print keys & values of an arrya in an HTML table. So, you should not skip any of the given if you really learn it.
Learn also –
PHP Admin Panel with Source code
PHP Image Gallery with database
How to print Array in PHP
When you are working with an array, then you may need to print its keys or values for debugging debug them & displaying them in an HTML table, list or any other format. So, It is most important to learn so that you can easily implement it in your project.
Top 4 Methods to print an Array
Now, You can print an array using the following four methods that gives array content their own format. So, you should use these methods according to your requirement.
To show you some examples, I have taken the following different types of array. After learning it, you can use your array to test your self.
Index Array –
$indexedArray = [123, "Noor Khan","Software Engineer", 25, "Patna" ];
Associative Array –
$associativeArray =[ "id"=>123, "full_name"=>"Noor Khan", "profile"=>"Software Engineer", "age"=>25, "city"=>"Patna" ];
Multi Dimensional Array –
$multiDimensionalArray = [ [123, "Noor Khan", "Software Engineer", 25, "Patna"], [124, "Sunil Kumar Yadav", "Electrical Engineer", 22, "Chennai"], [125, "Monika Singh", "Doctor", 18, "Jaipur"], [126, "Rapsan Jani", "Teacher", 28, "Mumbai"] ];
Print Array using print_r()
print_r() – If you need to prints an array with keys & values in an actual format that can be readable by humans. then you can use it.
Index Array
Use this code to print indexed array using var_expert()
print_r($indexedArray);
Output –
Array ( [0] => 123 [1] => Noor Khan [2] => Software Engineer [3] => 25 [4] => Patna )
Associative Array –
Use this code to print Associative Array using var_expert()
print_r($associativeArray);
Output –
Array ( [id] => 123 [full_name] => Noor Khan [profile] => Software Engineer [age] => 25 [city] => Patna )
Multidimensional Array –
Even use this code to print Multi dimensional Array using var_expert()
print_r($multiDimensionalArray)
Output –
Array ( [0] => Array ( [0] => 123 [1] => Noor Khan [2] => Software Engineer [3] => 25 [4] => Patna ) [1] => Array ( [0] => 124 [1] => Sunil Kumar Yadav [2] => Electrical Engineer [3] => 22 [4] => Chennai ) [2] => Array ( [0] => 125 [1] => Monika Singh [2] => Doctor [3] => 18 [4] => Jaipur ) [3] => Array ( [0] => 126 [1] => Rapsan Jani [2] => Teacher [3] => 28 [4] => Mumbai ) )
Print Array using var_expert()
var_expert() – If you need to print an array with keys & values in an associative array format, then you can use it
Indexed Array –
Use this code to print indexed array using var_expert()
var_expert($indexedArray);
Output –
array ( 0 => 123, 1 => 'Noor Khan', 2 => 'Software Engineer', 3 => 25, 4 => 'Patna', )
Associative Array –
Use this code to print Associative Array using var_expert()
var_expert($associativeArray);
Output –
array ( 'id' => 123, 'full_name' => 'Noor Khan', 'profile' => 'Software Engineer', 'age' => 25, 'city' => 'Patna', )
Multidimentional Array
Even use this code to print Multi dimensional Array using var_expert()
var_expert($multiDimensionalArray)
Output –
array ( 0 => array ( 0 => 123, 1 => 'Noor Khan', 2 => 'Software Engineer', 3 => 25, 4 => 'Patna', ), 1 => array ( 0 => 124, 1 => 'Sunil Kumar Yadav', 2 => 'Electrical Engineer', 3 => 22, 4 => 'Chennai', ), 2 => array ( 0 => 125, 1 => 'Monika Singh', 2 => 'Doctor', 3 => 18, 4 => 'Jaipur', ), 3 => array ( 0 => 126, 1 => 'Rapsan Jani', 2 => 'Teacher', 3 => 28, 4 => 'Mumbai', ), )
Print Array using var_dump()
var_dump() – If you need to print an array with keys, values and also datatype of each value, then you can use it.
Indexed Array –
Use this code to print indexed array using var_dump()
var_dump($indexedArray);
Output –
array(5) { [0]=> int(123) [1]=> string(9) "Noor Khan" [2]=> string(17) "Software Engineer" [3]=> int(25) [4]=> string(5) "Patna" }
Associative Array –
Use this code to print Associative Array using var_dump()
var_dump($associativeArray);
output –
array(5) { ["id"]=> int(123) ["full_name"]=> string(9) "Noor Khan" ["profile"]=> string(17) "Software Engineer" ["age"]=> int(25) ["city"]=> string(5) "Patna" }
Multidimensional Array –
Even use this code to print Multi dimensional Array using var_dump()
var_dump($multiDimensionalArray)
Output –
array(4) { [0]=> array(5) { [0]=> int(123) [1]=> string(9) "Noor Khan" [2]=> string(17) "Software Engineer" [3]=> int(25) [4]=> string(5) "Patna" } [1]=> array(5) { [0]=> int(124) [1]=> string(17) "Sunil Kumar Yadav" [2]=> string(19) "Electrical Engineer" [3]=> int(22) [4]=> string(7) "Chennai" } [2]=> array(5) { [0]=> int(125) [1]=> string(12) "Monika Singh" [2]=> string(6) "Doctor" [3]=> int(18) [4]=> string(6) "Jaipur" } [3]=> array(5) { [0]=> int(126) [1]=> string(11) "Rapsan Jani" [2]=> string(7) "Teacher" [3]=> int(28) [4]=> string(6) "Mumbai" } }
Print an Array using echo
echo – If you need to print an array with keys or values according to your own custom format, then you can use it. But you will have to use for loop or foreach loop.
here I will show you to print an array in a custom format. after learning it, You can easily print it in an HTML table, list, & another format that you want.
Indexed Array –
use this code to print an indexed array using for loop
for($key= 0; $key< count($indexedArray); $key++){ echo $key." -> ".$indexedArray[$key]; echo "<br>"; }
Use this code to print an indexed array using foreach loop
foreach ($indexedArray as $key => $value) { echo $key." -> ".$value; echo "<br>"; }
Output –
0 -> 123 1 -> Noor Khan 2 -> Software Engineer 3 -> 25 4 -> Patna
Associative Array –
use this code to print an Associative array using for loop
var_dump($associativeArray); foreach ($associativeArray as $key => $value) { echo $key." -> ".$value; echo "<br>"; }
Output –
id -> 123 full_name -> Noor Khan profile -> Software Engineer age -> 25 city -> Patna
Multidimensional Array
use this code to print multidimensional array using foreach loop
foreach ($multiDimensionalArray as $key => $value) { echo "<p><b>Array -". $key."</b></p>"; echo "<ul>"; foreach ($value as $k => $val) { echo "<li>".$k." - ".$val."</li>"; } echo "</ul>"; }
Output –
Array -0 0 - 123 1 - Noor Khan 2 - Software Engineer 3 - 25 4 - Patna Array -1 0 - 124 1 - Sunil Kumar Yadav 2 - Electrical Engineer 3 - 22 4 - Chennai Array -2 0 - 125 1 - Monika Singh 2 - Doctor 3 - 18 4 - Jaipur Array -3 0 - 126 1 - Rapsan Jani 2 - Teacher 3 - 28 4 - Mumbai
Print values of an array in PHP
Now, You also may need to print only values of an array in HTML list or table or any other format. So, you should learn it form the next steps –
Let’s take the following different types of array to print their values in HTML list.
Indexed Array –
$courseIndexed =[101, "PHP tutorials", "Rapsan Jani", 50];
Associative Array –
$courseAssoc = [ "id"=>101, "tutorials"=>"PHP Tutorials", "tutor" =>"Rapsan Jani", "fees" => 50 ];
Multidimensional Indexed Array –
// multidimensional Indexed Array $courseMultiDimenIndexed = [ [101, "PHP Tutorial", "Rapsan Jani", 40], [102, "React Tutorial", "Noor Khan", 80], [103, "JavaScript Tutorial", "Sunil Kumar", 60], [104, "Node.js Tutorial", "Monika Singh", 120], [105, "Angular Tutorial", "Amit Kumar", 150] ];
Multidimensional Associative –
$courseMultiDimenAssoc = [ "id" =>[101, 102, 103, 104, 105], "tutorial" =>["PHP tutorial", "React Tutorial", "JavaScript Tutorial", "Node.js Tutorial", "Angular Tutorial"], "tutor" =>["Rapsan Jani", "Noor Khan", "Sunil Kumar", "Monika Singh", "Amit Kumar"], "fees" =>[40, 80, 60, 120, 150], ];
Print values of a PHP Array using For loop
Use this code to print values of Indexed array using for loop
echo "<ul>"; for($k=0; $k<count($courseIndexed); $k++){ echo "<li>".$courseIndexed[$k]."</li>"; } echo "</ul>";
Use this code to print values of multi dimensional Indexed array using for loop
for ($row = 0; $row < count($courseMultiDimenIndexed); $row++) { echo "<p><b>Row number $row</b></p>"; echo "<ul>"; for ($col = 0; $col < count($courseMultiDimenIndexed[$row]); $col++) { echo "<li>".$courseMultiDimenIndexed[$row][$col]."</li>"; } echo "</ul>"; }
Print values of a PHP Array using foreach loop
Use this code to print values of indexed array using foreach
echo "<ul>"; foreach($courseIndexed as $value){ echo "<li>".$value."</li>"; } echo "</ul>";
Use this code to print values of associative array using foreach
foreach($courseAssoc as $value){ echo "<li>".$value."</li>"; } echo "</ul>";
Use thise code to print values of multidimensional Indexed Array using foreach
foreach ($courseMultiDimenIndexed as $value) { $row=1; echo "<p><b>Row -". $row."</b></p>"; echo "<ul>"; foreach ($value as $val) { echo "<li>".$val."</li>"; } echo "</ul>"; $row++; }
Use this code to print values of multidimensional Associative array using foreach
foreach ( $courseMultiDimenAssoc as $key=>$value) { echo "<p><b>". $key."</b></p>"; echo "<ul>"; foreach ($value as $val) { echo "<li>".$val."</li>"; } echo "</ul>"; }