Print an array in PHP
In this post, we will see how to print an array in PHP.
There are several ways to print contents of an array in PHP:
1. Using print_r() function
The most common method to print the contents of an array is using the print_r() function, which displays the human information about a variable. For an array, the values are displayed in a format that shows keys and elements.
The following code demonstrates this by printing an associative array using the print_r() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php $arr = array ('r' => 'red', 'g' => 'green', 'b' => array ('blue', 'brown', 'black')); print_r($arr); /* Output: Array ( [r] => red [g] => green [b] => Array ( [0] => blue [1] => brown [2] => black ) ) */ ?> |
Here’s how the output would look like for a normal array,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $arr = [1, 2, 3, 4, 5]; print_r($arr); /* Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) */ ?> |
Note that you can capture the output of the print_r() function by setting the second parameter to true. Now the print_r() function will return the output rather than printing it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php $arr = array ('r' => 'red', 'g' => 'green', 'b' => array ('blue', 'brown', 'black')); $str = print_r ($arr, true); echo $str; /* Output: Array ( [r] => red [g] => green [b] => Array ( [0] => blue [1] => brown [2] => black ) ) */ ?> |
2. Using var_export() function
The var_export() function outputs or returns a parsable string representation of a variable, which is valid PHP code. It is often used by programmers to print an array in PHP.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $arr = array ('r' => 'red', 'g' => 'green', 'b' => array ('blue', 'brown', 'black')); var_export($arr); /* Output: array ( 'r' => 'red', 'g' => 'green', 'b' => array ( 0 => 'blue', 1 => 'brown', 2 => 'black', ), ) */ ?> |
Here’s how the output would look like for a normal array,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php $arr = [1, 2, 3, 4, 5]; var_export($arr); /* Output: array ( 0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5, ) */ ?> |
3. Using var_dump() function
Another good solution to dump information about the contents of an array is using the var_dump() function. For an array, it recursively displays structured information about it such as its data type, value, and length.
The following code demonstrates this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php $arr = array ('r' => 'red', 'g' => 'green', 'b' => array ('blue', 'brown', 'black')); var_dump($arr); /* Output: array(3) { ["r"]=> string(3) "red" ["g"]=> string(5) "green" ["b"]=> array(3) { [0]=> string(4) "blue" [1]=> string(5) "brown" [2]=> string(5) "black" } } */ ?> |
Here’s how the output would look like for a normal array,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php $arr = [1, 2, 3, 4, 5]; var_dump($arr); /* Output: array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) } */ ?> |
4. Using json_encode() function
The json_encode() function returns a string containing the JSON representation of a value. It can be used to encode an array in PHP, resulting in a human-readable JSON string.
Here’s how the output would look like for an associative array,
|
1 2 3 4 5 6 7 8 |
<?php $arr = array ('r' => 'red', 'g' => 'green', 'b' => array ('blue', 'brown', 'black')); echo json_encode($arr); /* Output: {"r":"red","g":"green","b":["blue","brown","black"]} */ ?> |
For a normal array, only the values are displayed.
|
1 2 3 4 5 6 7 8 |
<?php $arr = array (1, 2, 3, 4, 5); echo json_encode($arr); /* Output: [1,2,3,4,5] */ ?> |
5. Using foreach construct
Finally, you can use the foreach construct to iterate over arrays.
The following code demonstrates this, where it assigns the value of the current element to $value on each iteration:
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php $arr = [1, 2, 3, 4, 5]; foreach ($arr as $value) { echo $value . ' '; } /* Output: 1 2 3 4 5 */ ?> |
The following code additionally assigns the current element’s key to the $key variable on each iteration.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $arr = array ('r' => 'red', 'g' => 'green', 'b' => 'blue'); foreach ($arr as $key => $value) { echo $key . "=>" . $value . "\n"; } /* Output: r => red g => green b => blue */ ?> |
Note: The whitespace is not preserved in the DOM, rather a single space is rendered in a browser. The solution is to wrap the output of the above functions inside a <pre></pre> tag.
That’s all about printing an array in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)