php

How to Print Array in PHP

Array variables are used to store multiple values in a single variable. Different types of programming problems can be solved easily by using array variables. Sometimes it requires checking the structure and values of the array variable in human-readable format for debugging purposes. The array values can be printed in PHP in different ways. Using a loop is the simplest way to print the array values. You can use two built-in functions of PHP to do this task. These are print_r() and var_dump(). If you want to get more detailed information about any array variable then you can use var_dump() because it provides information on array values by including data types. How you can use the ‘for‘ or ‘foreach’ loop and the built-in functions in PHP have been shown in this tutorial using multiple examples.

Using For Loop:

Using the ‘for’ loop is the simplest way to print the array values and the way of printing array values using the ‘for’ loop has been shown in the following example.

Example-1: Print Array Using For Loop

Create a PHP file with the following script. Here, a numeric array of string values has been declared. The count() function has been used to count the total number of array that has been used for the termination condition of the loop. Each value of the array will be printed in a newline.

<?php
//Declare an array
$MyArray = array("Ubuntu 20", "Windows 11", "Debian 10", "Mint 20", "CentOS 8");  
echo "<b>The array values are:</b><br/>";
//Print the array values using loop
for($i = 0; $i < count($MyArray); $i += 1)
{
    echo  $MyArray[$i]. "<br/>";
}
?>

Output:

The following output will appear after executing the above script.

Using Foreach Loop:

Using the ‘foreach’ loop is another way of printing the array values. The ‘foreach’ loop iterates through each element of the array. It is the simplest way to fetch the element of any type of array.

Example-2: Print Array Using the Foreach Loop

Create a PHP file with the following script. Here, an associative numeric array of number values has been declared and the foreach loop has been used to find out the highest and lowest values of the array.

<?php
//Declare the array
$Marks = array("0112838"=>97,"0144345"=>76,"0198583"=>88,
              "0128694"=>74,"0118574"=>82,"0114523"=>91);
//Initialize the highest and lowest values
$highest = 0;
$lowest = 101;

//Iterate the array values using foreach loop
foreach($Marks as $value){
    //Check the highest value
    if($highest < $value)
        $highest = $value;
    //Check the lowest value
    if($lowest > $value)
        $lowest = $value;
}
//Print the highest and lowest values
echo "The highest mark is:<b>$highest</b><br/>";
echo "The lowest mark is:<b>$lowest</b><br/>";
?>

Output:

The following output will appear after executing the above script.

Using print_r():

This function displays human-readable information of any variable. The syntax of this function is given below.

mixed print_r ( mixed $output [, bool $return = FALSE ] )

It has one mixed type mandatory parameter and one Boolean optional parameter. The default value of the optional parameter is false. If the value of the optional parameter is set to true, then the output of the function will return to a variable rather than print to the screen. This function can be used on different types of variables. In this tutorial, it is used to display the structure of the array variable. Some examples of using the print_r() function for displaying array values has been shown below.

Example-1: Using print_r() Function Without Optional Parameter

Create a PHP file with the following script. The optional parameter has not used in this example. So, the output will be printed on the browser.

<?php
//Declare the array
$myarr = array("Name" => "Linuxhint.com", "type" => "tutorial site","content" => array("Ubuntu","CentOS","Debian"));
//print the structure of the array
print_r($myarr);
?>

Output:

Open the browser and run the script from the server. The following output will appear after running the script from the server.

Example-2: Using print_r() Function Without Optional Parameter

Create a PHP file with the following script. The optional parameter is used in this example and it has set to true. So, the output will be returned to the variable, $output. The variable has been printed later.

<?php
//Declare the array
$myarr = array("courseId" => "303", "courseName" => "PHP","duratuon" => "6 Months");
//Store return value
$output = print_r($myarr,true);
//Print the return value
echo $output;  
?>

Output:

The following output will appear after running the script from the server.

Example-3: Using print_r() Function with the <pre> Tag

You can print the output of print_r() function in more readable way by using <pre> tag. Create a PHP file with the following script to know the use print_r() function with the <pre> tag.

<?php
// Declare array variable
$myarr = array("0" => "linuxhint.com", "1" => "is", "2" => "a", "3" => "good", "4" => "tutorial", "5" => "blog", "6" => "site");
// Store the output of print_r() function
$output = print_r($myarr,true);
//Add the starting pre tag of html
echo "<pre>";
//Print output
echo $output;
//Add the ending pre tag of html
echo "</pre>";
?>

Output:

The following output will appear after running the script from the server.

Using var_dump():

var_dump() function is used also to display the structured information of any variable. If you want to know about the data type of each element of an array variable then you can use this function. The syntax of this function is given below.

void var_dump ( mixed $output [, mixed $... ] )

It has one mixed type mandatory parameter and one mixed type optional parameter. This function doesn’t return any value.

Example-1: Using var_dump() Function for Numeric Array

Create a PHP file with the following script. Here, a simple numeric array has been declared and the structure of the array has been printed by using var_dump() function.

<?php
//Declare the array
$books = array("Learning HTML 5", "JavaScript basics", "Learning CCS3" ,"PHP 7 and MySQL 5", "JQuery", "Pro AngularJS");
//Print the structure of the array with data type
var_dump($books);
?>

Output:

The following output will appear after running the script from the server.

Example-2: Using var_dump() Function for Associative Array

Create a PHP file with the following script. Two associative arrays have been declared in the script and the structure of both arrays has been printed by using var_dump() function.

<?php
//Declare two arrays
$product_list1 = array("Dell Laptop" => 540, "Samsung Monitor" => 70, "Keyboard" => 15,"Mouse" => 5);
$product_list2 = array("TV" => 660, "Freezer" => 700, "Microwave Oven" => 200, "Speaker" => 50);
//Add the starting pre tag of html
echo "<pre>";
//Print the structure of both arrays
var_dump($product_list1, $product_list2);
//Add the ending pre tag of html
echo "</pre>";
?>

Output:

The following output will appear after running the script from the server.

Example-3: Using var_dump() Function for Multi-Dimensional Array

Create a PHP file with the following script to find out the difference between print_r() and var_dump() function. Here, one multidimensional array has been declared and printed by using both print_r() and var_dump() functions.

<?php
//Declare a multidimensional array
$students =
array("1109" => array("Name" => "John Paul", "department" =>"BBA", "Batch" => "100th"),
          "1274" => array("Name" => "William", "department" =>"EEE", "Batch" => "110th"),        
          "1703" => array("Name" => "Fahmida Yesmin", "department" =>"CSE", "Batch" => "54th"),  );

//Add the starting pre tag of html
echo "<pre>";
//The output of print_r()
print_r($students);
//The output of var_dump()
var_dump($students);
//Add the ending pre tag of html
echo "</pre>";
?>

Output:

The following output will appear after running the script from the server. The difference of these functions will be cleared if you show the output of both functions for the same array variable.

Example-4: Using var_dump() Inside Another var_dump()

Create a PHP file with the following script to know the way of using the var_dump() function inside another var_dump() function.

<?php
//Declare an array
$MyArray = array(20, 11, 10, 67, 8);  
//Using nested var_dump() function
var_dump(var_dump("Linux", FALSE, 67.03, 100, $MyArray));
?>

Output:

The following output will appear after running the script from the server.

Conclusion

Different ways of printing PHP arrays have been shown in this tutorial. The array values can be printed without a built-in function and with the built-in function. The uses of print_r() and var_dump() functions have been explained in detail here. The PHP developer can use these functions for debugging when any array variable is not displaying the expected output.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.