PHP How to Print Array (6 Methods)

php how to print array

In this PHP tutorial, you’ll learn how to print an array. You’ll see all 6 easy methods with examples.

You will know how to print array values in different ways using these methods. Whether it’s an indexed array, associative array, or multidimensional array, you’ll see each of them in this all-in-one guide.

Arrays are very useful when we want to store multiple values in one variable. And in PHP, we have various methods to print arrays. And we’re going to discuss all that in this tutorial.

So, let’s begin.

In PHP, We use the Following 6 Methods to Print Arrays :

Also, you will learn each method with an example. Not just simple indexed arrays, examples to print associative arrays, and multi-dimensional arrays are also included in this guide.

Tip: Don’t know how to run PHP on your PC? See this guide on how to run PHP code.

So, let’s begin with the first method.

1. Print an Array using for loop

Using for loop is the most suitable way to print PHP array content. By using a loop, you can print all the values of an array at the same time in different ways.

For example, you can use this PHP method to print an array line by line. Also, you can print each array element on a new line or inside the HTML table.

Now, let’s see the example.

Example 1.1:

In this example, I will create an indexed array with the name $students.

<?php
// an indexed array
$students = array("mike", "john", "rocky", "scarlett");
 
?>

Now, let’s print all the values of an array using for loop.

<?php
// an indexed array
$students = array("mike", "john", "rocky", "scarlett");
// storing array length in a variable
$arrlength = count($students);
// creating for loop
for($i = 0; $i < $arrlength; $i++){
  echo $students[$i];
  echo "<br>";
}
 
?>

On line no. 5, in the above code, I used the count() function to get the length of an array in numbers and stored it in a variable ($arrlength). The length of the above-indexed array is 4 which starts from 0. {0, 1, 2, 3}.

Then, I used the loop, which is a simple loop that runs a counter. Inside it, I created a variable ($i) that will run as a counter. The value of this variable increments every time when the loop runs.

Inside the parenthesis, I used the array name with the counter as $students[$i]. The value of $i is causing the array values to print on the screen one by one.

The above code output will look like this in your browser.

Code Output:

mike
john
rocky
scarlett

You can also mix it with some HTML and CSS to change the look of the above output. For example, you can display the above output inside an HTML table.

But what if you want PHP to print not just values but the named keys of an associative array? For that, we can use a for-each loop. Let’s see how to do it.

2. Print an Array using the Foreach loop

In PHP, we mostly use a Foreach loop to print associative arrays. Remember, a Foreach loop is made for arrays only. It helps a lot when we work with database tables.

Now, let’s see how to use it.

Example 2.1: This example will show you PHP code to print an associative array using the Foreach loop.

First, I will create an associative array. Then, I will use PHP code to print this associative array using for each loop. Now, see the code below.

<?php
// associative array
$students = array(
  "mike" => 24,
  "john" => 28,
  "rocky" => 30,
  "scarlett" => 26
);
// printing associative array using foreach loop
foreach($students as $key => $value){
    echo "$key's age = $value. <br><br>";
}
?>
Code Output:

mike's age = 24.

john's age = 28.

rocky's age = 30.

scarlett's age = 26.

Code Explanation:

  • In the above code, from line no. 3 to 8 I created an associative array. And the indentation you’re seeing is optional, you can write the associative array on just 1 line.
  • On lines no. 10 to 12, I created a loop. In that Foreach loop, I used the array name ($students) with $key and $value variables.
  • You can give any name to $key and $value variables, these variables will hold the value of named indexes and their values. We use these variables inside the loop to print the keys and values of an associative array.

This is how we can do this. But there is an echo statement that we use mostly to print a single array element. The echo statement is useful in some cases, now let’s see that in the next method.

3. Use echo Statement

In PHP, whenever we want to print something, we use echo statements. While in terms of arrays, echo is not the only solution but it is still helpful in many cases.

For example, echo helps a lot when we want to print a single value of an array.

Now, let’s see the PHP code to print an array using echo statements.

Example 3.1: This example will show you how to use an echo statement to print all the values of an array one by one.

<?php
// an indexed array
$students = array("mike", "john", "rocky", "scarlett");
// print array elements using echo
echo $students[0] . "<br>";
echo $students[1] . "<br>";
echo $students[2] . "<br>";
echo $students[3] . "<br>";
?>

Code Explanation:

If you see from lines no. 5 to 8, in the above code. I used an echo statement with an array name and index number on each line. And also, I added a <br> tag to display each value of an array on a new line.

Remember, the index number of an array starts from 0. We can access each element of an array using its index number.

The above code output will look like this in your browser:

Code Output:

mike
john
rocky
scarlett

Sometimes, we need to display array values in different ways like inside HTML tables. In that case echo statement really helps. But when the array is too long, then we use loops and other methods to display it.

Let’s see the next method.

4. Print PHP Array using print_r() function

The print_r() function is mostly used for testing purposes. It works with both variables and arrays. This function print PHP arrays in a structured format.

The syntax is very simple, we use the array name as a parameter inside this function. The print_r() has two parameters but the second parameter is optional. It is TRUE and FALSE.

Now, Let’s see the code example.

Example 4.1: This example shows how to use the print_r() function to print an array.

<?php
// an indexed array
$students = array("mike", "john", "rocky", "scarlett");
 
// print array using using print_r() function
print_r($students);
?>
Code Output:

Array ( [0] => mike [1] => john [2] => rocky [3] => scarlett )

Code Explanation:

In the above code, I used the print_r() function with the array name ($students) as a parameter. If you see the output of the code, the array is displayed in a structured format with values and index numbers on a single line.

We can also use HTML <pre> tags, to make it more readable, like in the given example below.

Example 4.2: In the following code, just see lines no. 6 to 8. We used pair of <pre> tags using an echo statement. This will print array content on separate lines instead of a single line.

<?php
// creating an associative array
$students = array("mike", "john", "rocky", "scarlett");
// print array using using print_r() function
 
echo "<pre>";
print_r($students);
echo "</pre>";
?>
Code Output:

Array
(
    [0] => mike
    [1] => john
    [2] => rocky
    [3] => scarlett
)

The above output is written clearly on separate lines and is readable.

The same PHP method applies to print associative arrays.

Example 4.3: This example shows how to print associative arrays using the print_r() function in PHP.

<?php
// creating an associative array
$students = array(
  "mike" => 24,
  "john" => 28,
  "rocky" => 30,
  "scarlett" => 26
);
// print associative array using using print_r() function
 
echo "<pre>";
print_r($students);
echo "</pre>";
?>
Code Output:

Array
(
    [mike] => 24
    [john] => 28
    [rocky] => 30
    [scarlett] => 26
)

The same method is used to print multi-dimensional arrays.

Now, let’s go ahead to our next PHP method to print an array.

5. Use var_dump() function

In PHP, we use the var_dump() function to dump information about one or more arrays or variables in a structured format.

The var_dump() prints some additional information about array elements. For example, the data type, index number, length, and value.

Let’s see the code example.

Example 5.1: This example shows PHP how to print an array using the var_dump() function.

<?php
// creating an associative array
$students = array(
  "mike" => 24,
  "john" => 28,
  "rocky" => 30,
  "scarlett" => 26
);
// print associative array using using var_dump() function
 
echo "<pre>";
var_dump($students);
echo "</pre>";
?>
Code Output:

array(4) {
  ["mike"]=>
  int(24)
  ["john"]=>
  int(28)
  ["rocky"]=>
  int(30)
  ["scarlett"]=>
  int(26)
}

In the above code, I used the var_dump() function with the array name inside the parenthesis ($students).

You can also print more than one array with the var_dump() function. In this case, just separate the array names by commas like the one below.

var_dump(array_name1, array_name2, array_name3, ....);

The same method applies to print indexed and multi-dimensional arrays.

Now, let’s see the last method.

6. Use var_export() function to Print Arrays

The var_export() function is almost similar to the var_dump() function. It outputs structured information about an array just like the previous function you’ve learned. But it does not return the data type of an array.

Example 6.1: This code example will show you how to use this function to print an array in PHP.

The syntax is completely similar to the previous function. All you have to do is just replace the name of the function.

<?php
// creating an associative array
$students = array(
  "mike" => 24,
  "john" => 28,
  "rocky" => 30,
  "scarlett" => 26
);
// print associative array using using var_export() function
 
echo "<pre>";
var_export($students);
echo "</pre>";
?>

If you see the above code, I used the same example as I used in the previous methods. The only thing I changed is just the function name.

funtions to print array

If you see the code output, the array is displayed in a structured format.

Code Output:

array (
  'mike' => 24,
  'john' => 28,
  'rocky' => 30,
  'scarlett' => 26,
)

That’s it.

So, this is how you can print an array in PHP.

Now, it’s your turn.

If you have any questions related to this PHP guide, feel free to ask those questions in the comment section below.

And don’t forget to share this guide on social media with your friends to help others.

Also learn: How to Upload Images in PHP and Store them in a Folder.

Leave a Comment

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

Scroll to Top