When working in php, we can print an array easily. To print arrays, you can use loops, print_r(), var_dump(), or json_encode().
The first example here shows how to print all the elements in an array with a loop.
$array = ["This","is","an","array"];
foreach($array as $ele) {
echo $ele . "n";
}
//Output:
This
is
an
array
You can also print arrays with the php print_r() function.
$array = ["This","is","an","array"];
print_r($array);
//Output:
Array
(
[0] => This
[1] => is
[2] => an
[3] => array
)
Another way to print a php array and the elements of array is with the var_dump() function.
$array = ["This","is","an","array"];
var_dump($array);
//Output:
array(4) {
[0]=>
string(4) "This"
[1]=>
string(2) "is"
[2]=>
string(2) "an"
[3]=>
string(5) "array"
}
One last way you can print an array is with the json_encode().
$array = ["This","is","an","array"];
echo json_encode($array);
//Output:
["This","is","an","array"]
When working with arrays in php, it is useful to be able to print the elements of an array.
There are many ways we can print the elements of an array in php.
If you want to get only the elements of the array, and not the array, you can loop over each element of an array and print it with echo. To print the elements of an array with php, we can use foreach to loop and print the elements of an array.
$array = ["This","is","an","array"];
foreach($array as $ele) {
echo $ele . "n";
}
//Output:
This
is
an
array
Printing All Elements of Array in php with print_r() Function
Another way we can print an array in php is with the php print_r() function.
print_r() prints human-readable information about a variable.
So, if you have an array, you can pass it to print_r() and get a human-readable array which shows all of the array keys and array values.
Below is an example in php showing how to print an array with print_r().
$array = ["This","is","an","array"];
print_r($array);
//Output:
Array
(
[0] => This
[1] => is
[2] => an
[3] => array
)
How to Print Array Elements in php with var_dump() Function
Another way we can print an array in php is with the php var_dump() function.
var_dump() returns information about a variable that includes its type and value.
So, if you have an array, you can pass it to var_dump() and get a data structure which has the type and value of each of the elements of the array.
Below is an example in php showing how to print an array with var_dump().
$array = ["This","is","an","array"];
var_dump($array);
//Output:
array(4) {
[0]=>
string(4) "This"
[1]=>
string(2) "is"
[2]=>
string(2) "an"
[3]=>
string(5) "array"
}
Printing Arrays in php with json_encode() Function
One last way we can print an array in php is with the php json_encode() function.
json_encode() returns the JSON representation of a variable. json_encode() is similar to the JavaScript stringify() function.
So, if you have an array, you can pass it to json_encode() and get the JSON representation of the array.
Below is an example in php showing how to print an array with the help of json_encode().
$array = ["This","is","an","array"];
echo json_encode($array);
//Output:
["This","is","an","array"]
Hopefully this article has been useful for you to learn the many ways of how to print an array and print the elements of an array in your php code.