|Language||Version||Operating System Host|
This guide will show you how to print out the values that are stored in your array using php. This is useful when you are debugging code or you want a quick way to show the data that is stored in your array. I frequently use
print_r when writing to code to see as I am manipulating my array that the data is changing as expected.
To print out the data of an array, we first need an array to work with. Here you can see I create an array called
$array and initialize it with three values in it:
# Here we have an array of data $array = array('value1','value2','value3');
Now that we have our array with values, we will then print out the data that is in our array using the
# Lets see what is in the array. print_r($array);
When we execute the code, we will see some output that looks like the following. This is telling us what values are in the array and their index within the array.
Array (  => value1  => value2  => value3 )
Now that we have seen the data that is in our array. If we want to output a specific item within the array we can echo it by using the index we see before the value in the
 brackets. For this example, lets output
value2 is seen in the
 index position, we will output it by doing the following.
#Lets output value2 echo $array;
As you can see it was pretty easy to print the array values in php. From there we were able to see the items in the array, and pick which we would like to output.
The full code might look something like below when we put it all together. There are so many great uses for
print_r, this was just one simple example. If you are having troubles or want to share what projects you work on that requires
print_r, please drop a comment below!
<?php # Here we have an array of data $array = array('value1','value2','value3'); # Lets see what is in the array. print_r($array); #Lets output value2 echo $array; ?>